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>
usingnamespace 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" ;
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.