Do if statements work this way? This is a “guess the number” game. The 1st if says to go higher/lower, the 2nd if says if you’re within a 50, 100 or 100+ range.
Both are supposed to work simultaneously, but I get an error.
Line 37 unexpected primary expression before ‘| |’ token, Line 38
expected ‘;’ before ‘cout’
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <cstdio>
using namespace std;
int main()
{
int x;
cout << "Please enter a number\n";
srand(time(0));
int y = rand();
while (x != y)
{
cin >> x;
{
if (!(cin.good())) //1st if
{
cout << "No letters noob" << endl;
cin.clear();
cin.sync();
}
else if (x < y)
cout << "Go higher" << endl;
else if (x > y)
cout << "Go lower" << endl;
else
cout << "You win!!" << endl;
}
{
if (y - x - 50 <= 0) || (x - y - 50 <= 0) //2nd if
cout << "within 50 range" << endl;
else if (y - x - 100 <= 0) || (x - y - 100 <= 0)
cout << "within 100 range" << endl;
else
cout << "100+ value away" << endl;
}
}
cin.get();
getchar();
return 0;
}
You are missing parentheses.
For example, this line:
Should read:
Because the entire if condition must be wrapped in parentheses.
Looks like you may have some other issues there as well.