First try at C++. I made a little text adventure "game".
It isn't much, but encouring feedback is appreciated:
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.
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:
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.Hahaha, sorry, will try it with hash tables and look into STL.
I also will try to work on my intendation.