#include <iostream>
usingnamespace std; // So the program can see cout and endl
int main()
{
int x=0, j=1;
do {
cout<<"Enter any number other than 5: ";
cin>>x;
cin.ignore();
if (x==5) {
cout<<"You were not supposed to enter 5";
}
elseif (j==10) {
cout<<"You are more patient than I thought...you win.";
}
j++;
} while(x!=5&&j<=10);
cin.get();
}
Hi, I am a little confused by the inconsistency of what the code says and what the program does. The underlined code tells the program to continue to run as long as j is LESS than or equal 10.. but it continues running until 10 iterations of the program have passed. Can anyone explain this? Thank you
★ Modify the program so that after 10 iterations if the user still hasn't entered 5 will tell the user "Wow, you're more patient then I am, you win." and exit.
Well, when I read the code it says as long as j IS 10 or IS LESS THAN 10. Since the program is Always less then 10 until the 10th iteration I began writing that part of the code as: j==10 and it wasn't until I cheated and looked at how they wrote it that I found out it's supposed to be less than or equal to 10. I wrote "==" because I want the program to stop on the 10th iteration not on less than 10 iterations, which is what I thought it would do but it doesn't... it works fine.
while ( j <= 10 ) means "if j is less than ( or equal to ) 10 continue with the loop, if it's greater stop" while ( j == 10 ) means "if j is equal to 10 continue with the loop, if it's anything else stop"
There's no inconsistency