I'm a beginner programmer in C++ trying to create this practice guess-the-number game that needs to fulfill some requirements. These are:
- Use 'flush' (I don't know what this is, help?)
- Exit the game once the number has been guessed
- Exit the game once a maximum of 10 guesses have been exceeded
But the problem right now is that when I enter a number, it tells me whether the entered number is higher or lower than the randomly generated correct number continuously. So if it's lower, the output is 'lowerlowerlowerlowerlowerlowerlowerlowerlower' and so forth. I don't know what I did wrong?
Here is my code. (X = randomly generated number, Y = guess, Z = # of guesses)
int main() {
int x , y, z;
x = rand() % 1000 + 1;
std::cin>>y;
do{
if (y > x){
std::cout<<"higher"<< std::endl;
z++;
} else if (y < x) {
std::cout<<"lower"<< std::endl;
z++;
} else if (y == x) {
std::cout<<"correct"<< std::endl;
}
} while (y != x && z < 11);
return 0;
}