while problem. please help

When i input 4, 2x the program exits, what is the problem? The program should exit only after i input 5.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#‎include‬ <iostream>
using namespace std;

int main()
{
int number;
cout <<" Test your luck, please enter a lucky number" << endl;
cin >> number;
while ( number !=5) {
cout << "Try it again" << endl;
cin >> number;
++number;
}
return 0;
}
I'm just learning c++ myself, but I think the "++num" is ticking your 4 over to a 5 which is why your program keeps ending. Perhaps you could attempt it in a different fashion.



While (cin>> number) //this will keep your program repeating,
{
If (number == 5)
Cout << "you lose, try again" ;

}
closed account (48T7M4Gy)
delete line 12
The problem is caused by line 12 because it incrementing the value of any number you put by 1.
And in this case since you are looking for only one answer at time, LOOPING statement is unnecessary, rather you can use decision making statement like IF statement.
Topic archived. No new replies allowed.