Random Guesser

I'm doing that guessing program where you need to guess a number between 1-100, but it seems I can't get it to compare the inputted guess against the random number. If anyone could help it would be greatly appreciated.

Line 15 and 17 give me a warning of "right operand of comma operator has no effect" and they also both give an error of "expected primary expression before '<<' token"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #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." or "Higher please."; << endl;}
    else {
            if (xg>x){cout << "Try a smaller number." or "Too low."; << endl;}
            else { if (xg==x){ cout << "You got it! Good job!";}}}
    return 0;
}
Last edited on
line 13,15,17 have embedded semicolons, so remove them.
there should be no semicolons in the middle of your statement.

eg; cout << item << item << item;

where you have cout << item << item; << item;
@Jaybob66 It really didn't affect the issue but thanks for the suggestion.
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
Last edited on
Topic archived. No new replies allowed.