Expected Primary-expression ERROR Pls Help!

Hello,

Thank you for any and all help in advance. I am fairly new to C++ and programming in general. I have been going through a C++ tutorial book and learned how to create a program to guess a number between 1-100 that is randomly generated by the computer. The program will tell you if the number is higher or lower. Once you get the right answer it will display how many tries it took.

Now I have to make a program on my own that switches roles. The Computer has to guess what number I (the user) is thinking and I give clues by saying high/low/correct.

As far as I can tell everything looks fine but on line 16 (int highNumber = (rand90 % 100 + 1) && (> guessedNumber); I am getting the error: |16|error: expected primary-expression before ‘>’ token|

Any ideas on what could be causing this. Again thanks for the help and my complete code will be below.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  #include <iostream>
#include <cstdlib>
#include <ctime>


using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
 srand(time(0));

 int guessedNumber = rand() % 100 + 1;
 int highNumber = (rand() % 100 + 1) && (> guessedNumber);
 int lowNumber = (rand() % 100 + 1) && (< guessedNumber);
 int tries = 0, guess;

 cout << "\tWelcome to the Guess Your Number Game!\n\n";
 cout << "The object of this game is to think of a number\n";
 cout << "between 1 and 100 and I will guess it.\n";
 cout << "If I guess your number in 4 tries or less I win, otherwise You win.\n";
 cout << "Are you ready?\n\n";
 cout << "If you are ready? (y/n): \n";

 string ready;
 string start;
 string response;

 cin >> ready;

 while (ready == ("n"))
    {
        cout << "What are you waiting for? I don't have all day.\n\n";
        cout << "Are you ready now? (y/n)";
        cin >> ready;
    }
    if (ready == ("y"))
        {
            cout << "Okay, go ahead and think of a number and press Y when you are ready\n";
            cin >> start;

            if (start == ("y"))
            {
                cout << "My first guess is: " << guessedNumber << endl;
                ++tries;
                cout << "Am I correct? (high/low/correct)";
                cin >> response;

                while (response == ("high"))
                {
                    cout << "My next guess is: " << lowNumber << endl;
                    ++tries;
                    cout << "Is that correct?\n";
                    cin >> response;
                }

                while (response == ("low"))
                {
                    cout << "My next guess is: " << highNumber << endl;
                    ++tries;
                    cout << "Is that correct?\n";
                    cin >> response;
                }

                if (response == ("correct"))
                {
                    cout << "Well congrats to me!!! It only took me" << tries << " guesses!\n";
                }
            }
        }

return 0;
}

What are you trying to do on that line? The > operator requires two operands and && is a logical operator, working with bool values (true or false values).
Logically I want the computer to generate a new random number but the number has to be greater than than the previous 'guessedNumber'. As well as the next line I want to be able to call a number that is less than the previous 'guessed number'

and I may just be thinking completely backwards as well. Again, I'm fairly new to programming so if there is another way I should be going about what I'm doing let me know.
Lines 16-17 aren't going to work the way you are thinking, even if they didn't cause compile errors.

Try something like the following to generate a random number within a specified range:
1
2
3
int random(int low, int high) 
{  return rand() % (high - low + 1) + low;
}



Last edited on
Topic archived. No new replies allowed.