Making the player go back to the same question

Apr 13, 2019 at 4:02pm
Hello, im trying to learn how to programm and im trying to make a little test now as my first project. I want to make it so if you get the answer wrong it throws you back and makes you try again until you get it right, what is the proper comand for it?

#include <iostream>
using namespace std;

int main()
{
int number;
cout << "solve 2+2= ";
cin >> number;
{
cout << "your answer was " << number << endl;
}
if (number == 4)
{
cout << "the answer " << number << " is right" << endl;
}
else
{
cout << "the answer " << number << " is wrong" << endl;
}

return 0;
}
Apr 13, 2019 at 4:26pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    const int correct_answer = 4 ;
    bool answered = false ;

    while( !answered )
    {
        int actual_answer = 0 ;
        std::cout << "2 + 2 ? " ;
        std::cin >> actual_answer ; // we assume that the user enters an integer

        answered = actual_answer == correct_answer ;
        std::cout << "the answer " << actual_answer << " is "
                  << ( answered ? "correct\n" : "wrong\n" ) ;
    }
}
Topic archived. No new replies allowed.