Very basic program. Need criticism.
Ok, first of all I am tottally new to C++ and programming in general. I am determined to learn C++. I have been using three diffrent books and been going VERY slowly as to not miss anything. I made this extremely simple program (at least to you veteran programers :P). I would just like some criticism on it as far as what I should have done different and what I did well. Thanks!
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
cout << "THE NUMBER GUESSING GAME!" << endl;
cout << "Guess a number 1-100" << endl;
cout << "You only have 5 guesses. Good Luck!" << endl;
cout << "Guess: ";
srand(time(0));
int ans = (rand() % 100) + 1;
int guess;
int done;
done = 0;
cin >> guess;
while(guess != ans && ++done < 5){
if (guess < 0 || guess > 100){
cout << "The number " << guess << " is not between 1-100." << endl;
}
else if (guess > ans){
cout << "The number " << guess << " is too high. You lose." << endl;
}
else if (guess < ans){
cout << "The number " << guess << " is too low. You lose." << endl;
}
cout << "Guess Again: ";
cin >> guess;
}
if (done == 5){
cout << "You ran out of guesses. The number was " << ans << "." << endl;
}
if (guess == ans){
cout << "YOU WIN" << endl;
}
system("PAUSE");
return 0;
}
# 1 Re: Very basic program. Need criticism.
First of all, please use code tags when posting code. They look like this
[ CODE] [ /CODE] (without the space after [)
And also, this belongs in the non-visual c++ forum. This is for questions related to VC++ IDE specific question. Questions about the C++ language itself shoudl go in the non-visual. Almost everyone new makes this mistake, someone should really clarify this in the forum descriptions
I left comments in following code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
cout << "THE NUMBER GUESSING GAME!" << endl;
cout << "Guess a number 1-100" << endl;
cout << "You only have 5 guesses. Good Luck!" << endl;
cout << "Guess: ";
srand(time(0));
int ans = (rand() % 100) + 1;
int guess;
int done;
done = 0; // you could have just done "int done=0;"
cin >> guess; //what happens if you dont write number?
while(guess != ans && ++done < 5){
//This only allows 4 guesses, ++done will return the value of done
// after the increment is evaluated. done++ will return the value of
// done before it is incremented
if (guess < 0 || guess > 100){ //you didnt say you were allowing 0
cout << "The number " << guess << " is not between 1-100." << endl;
}
else if (guess > ans){
cout << "The number " << guess << " is too high. You lose." << endl;
}
else if (guess < ans){
cout << "The number " << guess << " is too low. You lose." << endl;
}
cout << "Guess Again: ";
cin >> guess;
}
if (done == 5){
cout << "You ran out of guesses. The number was " << ans << "." << endl;
}
if (guess == ans){
cout << "YOU WIN" << endl;
}
system("PAUSE");
return 0;
}
Pretty good overall :). Had fun reading this, since this was exactly what my first C++ program was.
# 2 Re: Very basic program. Need criticism.
Sorry about the wrong forum. Ill post in the other next time. The only part of your comments I didn't agrre with was to put the ++ after done instead of before. If I put it after it runs 6 times. Before it runs the 5 I want. Also, for the pat with "cin >> guess" you asked me what happens if a number isnt input. How can I prevent this?
Thanks again
# 3 Re: Very basic program. Need criticism.
Sorry about the wrong forum. Ill post in the other next time. The only part of your comments I didn't agrre with was to put the ++ after done instead of before.So get rid of it and just increment "done" in the while loop.
int done = 0;
while (guess != ans && done < 5)
{
//...
++done;
}
Now there is no doubt the maximum times the while() loop will run.
Also, for the pat with "cin >> guess" you asked me what happens if a number isnt input. How can I prevent this? Read in as a string, convert the string to a number using strtol(), or you can check to see if any character in the string is not a digit and convert using atoi() if the string is a valid number.
Regards,
Paul McKenzie
# 4 Re: Very basic program. Need criticism.
Just a small point of criticism: cout << "THE NUMBER GUESSING GAME!" << endl;
cout << "Guess a number 1-100" << endl;
cout << "You only have 5 guesses. Good Luck!" << endl;
cout << "Guess: ";std::endl writes a linefeed and flushes the buffer. So above you are flushing the buffer superfluously between every line, which makes your program slower than it could be. Of course not an issue for a game like this, also not an issue if you are outputting 10 lines of something, but you might later write programs that create output and are performance critical, so remember that flushing is expensive.
On the other hand you don't flush the buffer after the last line, which you should to be sure your program works (the user sees what is expected from him) on all platforms. So better version: cout << "THE NUMBER GUESSING GAME!\n"
<< "Guess a number 1-100\n"
<< "You only have 5 guesses. Good Luck!\n"
<< "Guess: " << flush;
treuss at 2007-11-11 4:05:01 >

# 5 Re: Very basic program. Need criticism.
So get rid of it and just increment "done" in the while loop.
int done = 0;
while (guess != ans && done < 5)
{
//...
++done;
}
Alternatively, use a for-loop:
for (int done=0; guess!=ans && done<5; ++done)
{
//...
}
treuss at 2007-11-11 4:06:01 >

# 6 Re: Very basic program. Need criticism.
if (guess < 0 || guess > 100){
cout << "The number " << guess << " is not between 1-100." << endl;
}
You probably mean
if(guess < 1 || guess > 100)
GCDEF at 2007-11-11 4:07:11 >

# 7 Re: Very basic program. Need criticism.
Hi all.
Apart from tecnhical details, that are well explained into post above, I would like to suggest this simple rule: write remarks into your code always.
There are many good reasons to do this: it grows code readeability, will be easier to alter the program later and so on.
You should write remarks when you're writing the code, no after: in this way you fix the logical steps you have to follow in order to develop your program; moreover, when you aren't able to write what you're going to do using a human language, probably you won't able to do it using a programming language.