Simple Beginners Program

Salutations!
I'm basically trying to go through all of the beginning problems that were posted here: http://www.cplusplus.com/forum/articles/12974/

The output i'm trying to achieve is:

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.


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
26
27
// 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>

using namespace 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;
}

why are you using x again?
x is always equal to x.
your code should look like this:
1
2
3
4
5
6
7
int y;
do
	{
		cout << "Enter another number as long as it is not " << x << "." << endl;
		cin >> y;

	} while (x != y);

Know that when you use the x variable again, you are actually changing the value itself.
Last edited on

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.

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
26
27
28
29
30
// 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>

using namespace 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;
}

Last edited on
@TheSLY
Oh! Okay, yeah, I tried that before but it wasn't really working for me.
I figured it out, Thank you for replying !

@CodeWriter
I understand what you're saying but I'm wondering if its mandatory to have

previousNumber = currentNumber ?

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.
@CodeWriter
Oh ! Okay, that makes sense. Thank you. I have it figured out.
Topic archived. No new replies allowed.