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!";
do while loop doesnt work that way. what your program does is it waits until you enter a number greater than num
1 2 3 4 5
do
{ // do this
cout<< "Guess the random number: ";
cin>> guess;
}while (guess < num);// when guess is less than num
then it says "sorry too low" and then it enters another loop
while (guess < num);
it has an empty statement so it does nothing and never reaches the rest of the code.
What you should probably do is in the do while loop make some if statements to check if the number is correct.