Make the user keep quessing

I'm suppose to write a code that ask the user to guess until the user picks the number 4. Once the user picks, the number the code keeps looping with the statement and won't ask the user again for a number. How do I get it to prompt the user again if they don't pick the number 4?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>
#include <cstdlib>
using namespace std;

int main(){

int userGuess = 0;
int answer = 4;

	cout << "Guess a number between 1 and 10: ";
	cin >> userGuess;
	
	while(userGuess >=1 || userGuess <= 10){
		if (userGuess == answer){
		cout << "You guessed it!" << endl;
	}
	 else if (userGuess > answer || userGuess < answer){
		cout << "Wrong! Try again." << endl;
	}
	else {
		cout << "Please follow the directions!" << endl;
	}
	}
	return 0;
}
you have the right idea.
here is how I would solve the problem.
let me know if you have any questions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
using namespace std;

int main(){
    
    int userGuess = 0;
    int answer = 4;
    
    cout << "Guess a number between 1 and 10: ";
    
    while(userGuess != answer){
        cin >> userGuess;

        if ((userGuess <=1 || userGuess >= 10))
            cout << "You are not following instuctions!" << endl;
    else
        cout << "wrong awnser! try again!" << endl;
    }
    cout << "You gessed correctly!" << endl;

    return 0;
}
@Bdanielz How do you get it to pose the question again? Because it is looping but it won't ask the question again.
You dont want to be using a while loop, but rather a do-while loop. Hopefully this code will help you understand -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int userGuess = 0;
int answer = 4;

	cout << "Guess a number between 1 and 10: ";

	do
	{
		cin >> userGuess;
		
		 if (userGuess < 1 || userGuess > 10)
			cout << "You are not following instuctions!" << endl;

		 else if (userGuess != answer)
			cout << "wrong awnser! try again!" << endl;
	}
	while (userGuess != answer);
	
	cout << "You gessed correctly!" << endl;


The reason as to why you want to use a do-while loop and not a while loop is because, You want the user to guess FIRST and then check if it is right or not.

Also, it is not supposed to look like ((userGuess <=1 || userGuess >= 10)) like @bdanielz suggested, but rather like this (userGuess < 1 || userGuess > 10)

Both the number 1 and 10 is between 1 and 10 (I sure hope so).
Last edited on
Thanks guys I got it. I had to do-while loop and then put the prompt after the "do{".
@calisabeth can you send an updated version of your code? i'm having trouble with a similar issue!

thank you!
Here you go @niknik2:

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

int main(){

int userGuess = 0;
int answer = 4;



do{
cout << "Guess a number between 1 and 10: ";
cin >> userGuess;

if (userGuess < 1 || userGuess >10)
cout << "Please follow the directions!" << endl;

else if(userGuess!= answer)
cout << "Wrong! Try again." << endl;
}
while(userGuess != answer);

cout << "You guessed it!" << endl;

return 0;
}
Last edited on
Topic archived. No new replies allowed.