[C++] First little "game" [FEEDBACK] (4)

1 Name: #!/usr/bin/anonymous : 2011-03-01 21:45 ID:+m3Umt6s

First try at C++. I made a little text adventure "game".
It isn't much, but encouring feedback is appreciated:

http://tinyurl.com/4hjfhnl

2 Name: #!/usr/bin/anonymous : 2011-03-02 06:26 ID:zj3TUUeC

DVD of "THE COVE" is being distributed free of charge in Sea Shepherd now.

Please contact the application method each mail address described in following URL.

http://www.seashepherd.org/contact/general-public.html

Sea Shepherd has already begun serving in Japan and each country.
You also must apply.

3 Name: #!/usr/bin/anonymous : 2011-03-02 22:54 ID:Q/M3jcGx

>>1

Why is that link a tinyurl?! Doesn't make any sense.

> http://tinyurl.com/4hjfhnl
> http://pastebin.com/0Gj2rVq8

Saved two letters there!

Anyway, with regards to the program itself, that'll get highly messy fast if you keep adding to the processing code like it is now. I'd highly recommend at least putting each individual case into a separate function, give them all the same prototype, then build a hash table of all those functions so that you can look up the function, if it exists, call it to do the processing, and if it doesn't, dump an error of some kind.
(Btw: C++ has that functionality in the STL. Look up hash_map.)

Second thought: don't use ints, use strings. Unless you're actually programming for touchtone phones, in which case learn how to do voice processing because even those don't use numbers for menus nowadays ;)
Then later on you can, for example, split on spaces and build your input prompts into a little mini-language.
(The STL has functionality to do all of that stuff, too.)

So putting those two together, here's a quick example...

#include <string>
#include <iostream>
#include <unordered_map>
using namespace std;

typedef void (*fptr) ();

// A few dummy functions
void first() { cout << "First function" << endl; }
void second() { cout << "Second function" << endl; }

int main()
{
unordered_map<string, fptr> commands; // <keytype, valuetype>
commands["first" ] = first;
commands["second"] = second;

string choice;

cout << "Enter command: " << flush;
cin >> choice;
if (commands[choice]) {
commands[choice]();
} else {
cout << "I don't know what that is!" << endl;
}

return 0;
}

(You'll need to use g++ -std=c++0x for this. I have no idea how other compilers work, or how well they support the 0x stuff, but I'm sure google knows.)

Other notes:

  • Please, please indent your code nicely. Pretty please, with sugar on top. Really. Badly indented code is about as close as ASCII text can look to the disorganized scrawlings of a madman.
  • Only call srand() once, at the start of main. After that you'll get decent random numbers as long as you don't fiddle with it.
  • You neither need nor want stdio.h if you're using iostream. Mixing C and C++ I/O functions will end up leading to very strange bugs since both of them do their own completely independent buffering. And since iostream knows how to deal with string and format C++ classes nicely, it's the way to go.

4 Name: #!/usr/bin/anonymous : 2011-03-03 17:59 ID:+m3Umt6s

Hahaha, sorry, will try it with hash tables and look into STL.
I also will try to work on my intendation.

This thread has been closed. You cannot post in this thread any longer.