I tried multiple times to win, but I can't! Is it just me or is my program flawed? Also I have a char variable named answer to ask if a user wants to play a game. Do I do this outside of the loop or within?
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
usingnamespace std;
int main ()
{
int guess;
char answer;
srand (time (0));
for (int i = 0; i < 1000; i++)
{
int random = rand( );
int num = ((random % 1000) + 1);
cout << "Guess a number between 1 and 1,000: ";
cin >> guess;
if (guess > num)
{
cout << "The number is lower. Guess again\n\n";
continue;
}
if (guess < num)
{
cout << "The number is higher. Guess again\n\n";
continue;
}
elseif (guess == num)
{
cout << "You've guessed correctly!\n\n";
break;
}
}
return 0;
}
Odd. I took lines 17 and 18 out and I got a debug error saying num is not being initalized. So then I did int guess, num; and I got the same error. It seems as though I need those 2 lines of code, my book had an example with a line very similar to that in it so I think I need it?
You weren't told to delete lines 17 and 18.
You were told to move them.
That said, the advice given you was not correct. The error is because you are using continue on lines 27 and 33. That means start the loop over. Don't do that.
Replace those two lines (line 27 and line 33) with cin >> guess;.
You may also want to tell the user she lost on line 43: cout << "Sorry, the number was " << num << ".\n\n";
Hope this helps.
[edit] Also, line 37 should not start with else.
[edit 2] But line 30 should start with else.
[edit 3] One last piece of advice. (Sorry.)
As it currently stands (given the corrections I advised you), the user only gets two chances to guess a number in 1 to 1,000. That's a pretty tough game.
You might want to increase the number of chances it takes to guess, and decrease the range of numbers to something like 1 to 100. (I tested it against 1 to 10. I won once out of eight tries.)
I think I want the user to keep guessing until he/she gets it right. I don't understand how the user only gets two chances to guess the number? It's a high number for a reason, it should be an infinite loop until the user guesses correctly.
Hmm, in that case, the advice originally given you was correct. Move lines 17 and 18 to between lines 13 and 14. Also, since you want an infinite loop, you can just say:
1 2 3 4 5 6 7 8 9 10
srand (time (0));
int random = rand( );
int num = ((random % 1000) + 1);
cout << "Guess a number between 1 and 1,000: ";
while (true) // infinite loop
{
cin >> guess;
...
Programming requires you to think like a programmer. A programmer is a problem solver, not someone who writes code. Writing code is simply a by-product.
Before you write any code you must ask youself what you need the code to do (what is the problem to solve), you don't simply start writing code and hope it turns into something (although sometimes this works if your really really really stoned :P)
Before writing any code, try to solve your problem in ENGLISH first, this makes writing code soooo much easier. eg. you want people to guess a number between 1 and 1000, and keep guessing until they get it right. So a pseudocode (english like) solution for this would be:
1 2 3 4 5 6 7 8 9 10 11
create a random number;
prompt user to enter data;
while the data being entered is not the same as the random number display the appropriate error message (higher \ lower) and get new input.
if number is correct display congratulatory message.
ask user if they would like to play again... if so start again. else we are done.
//end
being that the user will probably not guess the number on the first try the actual code for "prompt user to enter data" will be placed inside the sentinel loop -> while the data being entered.... so that we don't duplicate code or write unnecessary code.