Need help with a random number game

Hey, im relatively new in c++ and im trying to code a Random number guessing game.

Heres the code-

#include <iostream>
#include <ctime> // to use the time function
#include <cstdlib>
using namespace std;

int main()
{
srand(time(0));
int n = rand() % 20;
cout<< "Pick a number 1 through 20: ";
int x;
cin>> x ;
while (x!=n)
if (x<n){
cout<<"Thats to low!";
}
if (x>n){
cout<<"Thats to high!";
}
if (x==n) {
cout<<"Correct!";
}
cin.get();
}



If you've tried this, well it doesn't work, it either loops forever, or doesn't loop at all, so any help?
I think you forgot the curly braces after your while around all those if statements. I think you also forgot to put a cin statement inside the loop to get new guesses.

In the future, could you please use [code] tags? They make programs a lot easier to read. :)

Have fun!

-Albatross
Last edited on
Topic archived. No new replies allowed.