I was doing the challenges on this website and came upon a problem
The code is supposed to ask you for a number other than 5
When you give it a number other than 5 it will ask again 10 times and if you haven't put 5 in those 10 tries it will give up but if you did input 5 it tells you you weren't supposed to do so and quits
Its sort of an annoying program
But my problem is that it doesn't quit after the 10nth try
Its supposed to update the variable and when it does the if statement takes over and the program gives up
But it doesn't and i cant really see anything wrong with it
#include <iostream>
usingnamespace std;
int main()
{
int num,x;
x=0;
cout<<"Enter any number but 5! "<<endl;
cin>>num;
while (num != 5){
cout<<"Enter any number but 5! "<<endl;
cin>>num;
x++;
}
if (x == 10){
cout<<"Wow, you're more patient than I am, you win."<<endl;
return 1;
}
if (num == 5){
cout<<"Hey! you weren't supposed to enter 5!"<<endl;
}
}
You accidentally put the if statement outside of the loop. The only way for the program to get to line 18 is to enter 5 (and thus, line 24 is redundant).