You're indentation is a bit off. The if statement on lines 29, 33, 37 and 41 should be in line with the one on line 25. You would only indent like you have done if each if-statement was inside the previous if-statements code block, or scope. The same applies to 46 through to 62.
Task
* Re-write the way you accept input to account for any errors that may occur. e.g. an int accepting a char.
** Re-write the program so that it is all in one loop.
use an if..elseif..elseif..else construct since each guess can only meet one of the prescribed conditions. Test your program by entering a character instead of a number and see what happens. Once you realize the problem, read the info at this link to fix the problem. http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3
Also, just to make it easier on the eyes, if you have one line of code after an if statement, for example:
1 2 3 4
if (playersGuess > 25 || playersGuess < 1)
{
cout << "You have to guess between 1 and 25.";
}
you can just write:
if (playersGuess > 25 || playersGuess < 1){cout << "You have to guess between 1 and 25.";}
Also, including an end of line statement might be something you'll want once this executes: if (playersGuess > 25 || playersGuess < 1){cout << "You have to guess between 1 and 25.\n";}
or if (playersGuess > 25 || playersGuess < 1){cout << "You have to guess between 1 and 25."<<endl;}
if (playersGuess > 25 || playersGuess < 1){cout << "You have to guess between 1 and 25.";}
This is definately not easier on the eyes. JustAWalrus, if you're going to only execute one command after your if-statement, you do not need the curly braces; however it does not hurt to do so.
stavros wrote:
What exactly is your question?
JustAWalrus wrote:
Any critiques on the code are welcome.
EDIT* kempofighter's post is very important and should not be overlooked!
I'd say that if (playersGuess > 25 || playersGuess < 1) would better not be part of the main control construct of your loop, as it shouldn't reduce your counter. My suggestion is something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for (int counter=0; counter<3; counter++)
{
while (true)
{
cout << "\n Guess: ";
cin >> playersGuess;
if (playersGuess > 25 || playersGuess < 1)
cout << "You have to guess between 1 and 25." << endl;
elsebreak;
}
//rest of the loop here...
}
If you read the link that I posted there is a way to test for valid input as well as a range. You can combine the two concepts with an embedded loop. So within the for loop you write an embedded while loop that asks for input until the user enters something valid. There is a small while loop in the example code that you can copy and paste easily. Then adapt it for the specific range test that you need.
Ah, right. To be honest, I didn't bother to click on the link kempofighter suggested, hahaha. I just read his post and assumed that the link only provided a method to check for non-numeric entries. Well, it turns out that my post is redundant... Just check kempofighter's link.
I don't usually +1 something, but kempofighter's link is possibly the most useful thing I have ever seen -- and I've seen table saws that refuse to cut your finger off!
Couple things. Since you added "using namespace std;" to the top of the page you don't need to put std:: before commands in that namespace. Also, add "#include <limits>" to the top of the page.
After "cin >> playersGuess;" add "cin.ignore();" to clear out the ENTER input.
As you may already know you can put multiple parameters into your cout << command. i.e. "cout << "\n The number was : " << actualNumber;"
Assuming you're not running the program in debug mode, you need to place a cin.get(); before return 0; or else the program will quit out on you.