Is this a good solution?

There is a beginner exercise that I found on this forum called, "while (user==gullible)", the following is the description of the program modification #2:
**Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e. on the first iteration ask, "Please enter any number other than 0", on second iteration, "Please enter any number other than 1", etc. The program must behave accordingly, that is, exiting when the user enters the number they were asked not to.

Since I couldn't figure out how to ask user like mentioned above this is my solution. But I would like to know how I can ask each time like mentioned above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
    int input, x; // where input represents user input and x represents number of iterations

    for (x=0; x<10;x++){ //need increment else the program won't run if statement when condition is met.
        cout << "Enter any number other than the number of times you have been asked to enter a number." << endl;
            cin >> input;
            if (input == x) {
                exit (0);
            }
}
    return 0;
}
Last edited on
You may want to consider the difference between the assignment operator =, and the equality comparison operator ==.
Yup thanks.
You're welcome :)
By the way do you know how I can ask for each statement instead of having 1 general statement. Eg, for first iteration cout << "please enter any number other than 0"; . And for second iteration cout << "please enter any number other than 1"; , etc.
Last edited on
cout << "please enter any number other than " << x << ": " ;
@cire thanks a lot it works!
Topic archived. No new replies allowed.