+ 1

C++ while problem

The program was meant to continue 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. However, if the user input isn't five, the while loop continues, not asking for input, but printing the phrase "Enter another number" over and over again. How do I get the program to work as intended? this is my program: #include <iostream> using namespace std; int input; int main() { cout << "Enter any number other than five!" << endl; cin >> input; while(input != 5) { cout << "Enter another number" << endl; cin >> input; } if (input == 5) { cout << "Hey, you weren't supposed to do that!" << endl; } return 0; }

26th Jun 2017, 3:55 PM
Somebody that you used to know
Somebody that you used to know - avatar
3 Answers
+ 1
Your program works as you are intending in its current state. I'm guessing that the issue you're having is with the code playground. You need to enter all inputs prior to submitting. as in: 4 3 2 1 5 Then press submit and the output will be as expected.
26th Jun 2017, 6:05 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@ChaoticDawg is correct. Try entering in all of your inputs prior to running it, if you are in fact running it on the code playground. Just make sure your last input is 5, or it will pull a Buzz Lightyear, "To infinity, AND BEYOND!"
26th Jun 2017, 7:17 PM
Zeke Williams
Zeke Williams - avatar
0
You have to declare your variable inside main function first. then try again and tell me if it continue
26th Jun 2017, 4:05 PM
Nura Programmer
Nura Programmer - avatar