user==gullible help with while loop

Hi. I'm extremely new to this. I'm trying to do the user==gullible exercise. This is the instruction.
While( user == gullible )
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)

Write a program that ccntinues to asks the user to enter any number other than 5 until the user enters the number 5.
Then tell the user "Hey! you weren't supposed to enter 5!" and exit the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
	int num;

	cout << "Enter a number other than 5. " << endl;
	cin >> num;

	while (num != 5)
	{
		cout << "Please enter another number. " << endl;
	}
	cout << "I told you not to enter 5. " << endl;

	return 0;
}

What I don't understand is when I debug this and I enter a number other than 5, it won't stop saying "Please enter another number." But, when I enter 5, it does it correctly and exits the program. What's wrong with my while loop?
1
2
3
4
	while (num != 5)
	{
		cout << "Please enter another number. " << endl;
	}

The program will keep looping within that loop, and num will never change, as there is no statement in the loop body to change it.

You should insert a cin statement to input num within the loop body.
zsteve! Thank you so much for that. So, putting the cin statement to input num again makes the loop stop and allows the user to input another number. I've been trying to figure out how to stop the loop. Thank you again.
You could try this for your while loop:

 
while(std::cout << "Enter a number other than 5: " && std::cin >> num && num != 5)
giblit. I was looking at your while loop. Out of curiosity, how would you output the false statement? I really don't know how to use your method. Also, when I just use the while loop, it doesn't loop until I enter 5. I think it stopped at 2 inputs. This is how I wrote the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{
	int num;

	cout << "Enter a number other than 5. " << endl;
	cin >> num;

	while (cout << "Enter a number other than 5: " && cin >> num && num != 5)

	return 0;
}

What am I doing wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main()
{
    int num;

    cout << "Enter a number other than 5: " ;

    while( cin >> num && num != 5 ) cout << "Please enter another number: " ;

    cout << "I told you not to enter 5.\n" ;
}
Thank you JLBorges. So, I only put the cin statement and while statement into the parantheses. Curiosity, I've been seeing this a lot "\n" What does that do?
A \ in a char or string literal introduces an 'escape sequence'. The '\' together with the character immediately after it is used to define a special character.

'\n' is not two chars, it is the single char representing a newline.

http://en.cppreference.com/w/cpp/language/escape
Thank you JLBorges.
Topic archived. No new replies allowed.