im done with the 2nd modification but I cant make the program exit when I enter 5.
This is the exercise I have been working on.
Write a program that ccontinues 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.
★ 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.
★★ Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int number, tries;
for (tries = 0; tries < 10; tries++)
{
cout << "Enter a number except 5: ";
cin >> number;
if (number == 5)
cout << "You cannot do that!";
}
cout << "okay im tired now";
}
In this case, it isn't a matter of exiting the program. What is really required is to exit from the loop.
There are a number of ways to do that. One way is to use a break; statement. Another way would be to make use of the loop condition, tries < 10. The loop will repeat while that condition is true. You might combine that with another condition, to also test the value of number. Or even change the value of tries, so the condition becomes false.