do....while loop problems

I'm very new to c++, and having alot of issues with a project I have to do. The project is

Write a program in which:
A random integer between 0 and 500 is generated at the beginning.
The user is asked to guess the number.
The user is told whether her/his guess was too high or too low, and asked to guess again, until the guess is correct.

I just cannot get the program to loop. When I run it, I enter a number and it says "Sorry, too low", and then nothing happens.

I'd really appreciate any help as I'm new to this and really want to learn! The code I have so far;


#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
int num, guess;
num = rand() % 500 ;


do{
cout<< "Guess the random number: ";
cin>> guess;}

while (guess < num);
cout<< "Sorry, too low.";

while (guess > num);
cout<< "Sorry, too high.";

while (guess != num);
cout<< "Congratulations, you got it!";

return 0;
}


by the way, not asking for the solution, just a few pointers about where i went wrong!! thanks.
Last edited on
You need to be in a loop until the correct number is found (not 2 loops). Use something like
1
2
3
do
..
while(guess != num)
Then inside the loop determine if guess's bigger or smaller than num and print the correct messages.
Topic archived. No new replies allowed.