Here is what it should look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
int main()
{
int xg;
int x = rand() % 100+1;
cout << "Let's play a game. Guess a whole number between 1 and 100." << endl;
cin >> xg;
if (xg < x)
{
cout << "Try guessing higher." << endl;
}
else
{
if (xg>x)
{
cout << "Try a smaller number." << endl;
}
else
{
if (xg==x)
{
cout << "You got it! Good job!";
}
}
}
return 0;
}
|
Your problem was first of all what Jaybob66 mentioned, you can have a semicolon before
<<
without reusing cout.
Second of all you did this:
cout << "Try guessing higher." or "Higher please."; << endl
the next step to correct this would be removing the semicolon:
cout << "Try guessing higher." or "Higher please." << endl
Finally, you did this
cout << "some string" or "some other string";
"or" isn't recognised by the operator << since it's undefined, it's up to you to chose which string you want to cout, not to the program.
So here is the final and working version of that line:
cout << "Try guessing higher." << endl;
Also, your code was hard to read because of your editing. I know people argue about this, but try to space your code out and not have it like this:
1 2 3 4 5
|
void function(){
for (/*[...]*/){
if(/*[...]*/){
//do something;
}}} //<- this is confusing
|
try this instead:
1 2 3 4 5 6 7 8 9 10
|
void function()
{
for (/*[...]*/)
{
if(/*[...]*/)
{
//do something;
}
}
}
|
Hope this helps,
Regards,
Hugo
EDIT: Also if you want to keep playing the game until you find the right number, and not have the program terminate after each guess, you should probably use a while loop