Click to See Complete Forum and Search --> : One of my first programs! Guessing game
mrblack
08-26-2004, 09:43 PM
Hello all;
I have decided to get back into C++. :) I am finding things pretty easy, as I took the dive into learning PHP a little while back. I dont know that much... variables, if's, while's... tell me what you think, and how I can improve it. :)
#include <iostream.h>
#include <stdlib.h>
void main(void)
{
unsigned short int guess, answer = 32, tries = 1;
cout << "You have 5 tries to reach the correct answer.\n\n";
cout << "First try:\t\t";
cin >> guess;
while ((guess != answer) && (tries < 5)) {
tries++;
if (guess < answer) {
cout << "Higher! Try No. " << tries << "\t";
} else {
cout << "Lower! Try No. " << tries << "\t";
}
cin >> guess;
}
if (guess == answer) {
cout << "\n\tYou win!\n\t--------\n\n";
} else {
cout << "\n\tYou lose!\n\t---------\n\n";
}
system("PAUSE");
}
bwkaz
08-27-2004, 04:41 PM
Well...
Change your #include <iostream.h> line to #include <iostream>, and then add a "using std::cout; using std::cin;".
system("PAUSE");? What, is this supposed to run on Windows only? ;) Try cout << "press return\n"; cin.getline(100); (or something like that; my C++ iostreams are a bit lacking).
Or just remove that completely -- there's no need to prompt for a keypress at the end of your program, since it's obviously a console program, and therefore you don't need to keep the shell running because your users will keep their shell running for you. They just want their shell prompt back without any unnecessary keystrokes. ;)
Good to see people having fun programming :cool:
I notice your program's answer is hardcoded. Perhaps you should make it a random number, then tell the user the answer at the end of your program.
Lookup the rand() and srand() function in your documentation. Both are declared within the stdlib.h header. Also, you'll want to look into the time() function located declared in the time.h header. the return value gives you the current system time, so using it as a seed value for srand will give you pretty good randomness.
Placing the following line at the top of your application should seed the randomness for your application sufficiently:
srand(time(NULL));
Have Fun!
mwinterberg
08-27-2004, 10:03 PM
In addition to what bwkaz said about <iostream>, you'll also want to C++-*** your usage of <stdlib.h> with <cstdlib> (although, your "usage" of stdlib.h isn't really there, unless you go the random route as stoe suggested). Since those existing functions are wrapped in the std namespace, you'll also have to prefix them with std:: as with cin and cout.
Your while loop can also be safer (i.e. not freaking out when someone enters in "Bob" as a guess). Sections 15.2 - 15.4 of the C++ FAQ Lite (found here: http://www.parashift.com/c++-faq-lite/input-output.html [well, that's section 15]) will be the most interesting for that.
For 100% more safety than bwkaz's cin.getline(100), you can use
std::string bob;
std::getline(std::cin, bob);
To prevent buffer overflows. I can't remember if it's standard or not, but I'm fairly certain GNU's STL is at least based off of SGI's (which contains it). It also looks like VC++ has it as well, so it should be decently portable.
One last thing,
void main(void);
isn't standard C, or C++. In C++, the standard for that function's declaration is:
int main();
and for C, it's the same, but with void in the parens. C++ has at the very least deprecated that, if not removed it entirely.
mrblack
08-28-2004, 02:57 PM
Thanks for the replies guys! Excellent feedback, looking up some stuff now. :)
For the record: I used Dev-C++ for Windows, but when I get my Linux box set up properly, Ill use GCC.
FYI, getline is standard.
mrblack
08-30-2004, 04:36 PM
OK guys, I re-wrote my program.
#include <iostream>
int main()
{
unsigned short int guess, answer = 32, tries = 1;
cout << "You have 5 tries to reach the correct answer.\n\n";
cout << "First try:\t\t";
cin >> guess;
while ((guess != answer) && (tries < 5)) {
tries++;
if (guess < answer) {
cout << "Higher! Try No. " << tries << "\t";
} else {
cout << "Lower! Try No. " << tries << "\t";
}
cin >> guess;
}
if (guess == answer) {
cout << "\n\tYou win!... ";
switch (tries) {
case 1:
cout << "and in 1 go? Cheater!";
break;
case 2:
cout << "damn 2 goes!... lucky shot.";
break;
case 3:
cout << "very nice, congratulations.";
break;
case 4:
cout << "not bad, but I've seen better.";
break;
case 5:
cout << "you just made it!";
break;
}
cout << "\n\n";
} else {
cout << "\n\tYou lose, sucka!";
cout << "\n\n";
}
return 0;
}
I tested it on a Linux box (I actually think its a FreeBSD box) via an SSH connection to my shell account at cyberspace.org.
CaptainPinko
09-02-2004, 03:14 PM
It won't change the way your programs runs but its a very important skill/habit and it'll help you down the road. Start commenting now and it'll pay off in spades later. Trust me.
Also if you are learning I suggest your try contest problems like http://www.ecoo.org/sigcs/past/index.html or here http://contest-cemc.uwaterloo.ca/ccc/past/previous_contests.shtml .
Doing this problems will force you to learn various parts of the language and train you to think about programming.
Good luck!
PS- Oh, and if you are just learning to program may I suggest starting with Java? similar to C++ but not as many subtle errors to make. and you can code it equally as well on Windows and Linux. I recommend NetBeans IDE (http://www.netbeans.org)