Enter a number:
5
Enter a number as long as it is not 5.
6
Enter a number as long as it is not 6.
6
Congrats! you win!
However, the output I get is:
Enter a number:
5
Enter a number as long as it is not 5:
4
Congrats! You win!
I figured if I used one variable, then I would basically just have it repeat as long as it is not equal to itself. Sadly, the output does not match up. So I don't understand why it doesn't work.
IF any of you are able to explain why it doesn't work or have anywhere I can look, then I would greatly appreciate it.
This is my first post on this site so if you have any criticism then please tell me so I may better myself.
// a revised while(user==gullible) program where they are asked to
// input a number first and then another number that is different than the one before,
// if they input the number the inputted before then they win.
#include <iostream>
usingnamespace std;
int main()
{
int x;
cout << "Enter a number. " << endl;
cin >> x;
do
{
cout << "Enter another number as long as it is not " << x << "." << endl;
cin >> x;
} while (x != x);
cout << "Congrats! You win! " << endl;
return 0;
}
It is a logical bug. You change the previous value with cin >> x and then compared it with itself.
You need two variables. The previous variable and the current variable.
// a revised while(user==gullible) program where they are asked to
// input a number first and then another number that is different
// than the one before, if they input the number the inputted before
// then they win.
#include <iostream>
usingnamespace std;
int main()
{
int previousNumber;
int currentNumber;
cout << "Enter a number. " << endl;
cin >> currentNumber;
do
{
previousNumber = currentNumber;
cout << "Enter another number as long as it is not " << previousNumber << "." << endl;
cin >> currentNumber;
} while (currentNumber != previousNumber);
cout << "Congrats! You win! " << endl;
return 0;
}
You have to update the previous number with the current number otherwise it would remain as the first number entered and no longer the previous number.