how do you exit a program after meeting a certain condition?

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>
using namespace 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";
	
}
Last edited on
return; from main to end the program.
1
2
3
4
5
6
if(number == 5)
    {
    cout << "You cannot do that!";
    cin.get();
    exit(0);
    }

Tells them they cant do that and closes when they press enter...

also click edit your first message, mark your code and click the <> button to make it a code like mine above, its just... nicer that way
Last edited on
thank you for replying guys. the program now works as intended although I dont understand the need for return and how the cin.get() works/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
using namespace 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!";
			cin.get();
			exit(0);
		}
		
	}
	
	cout << "okay im tired now";
	
	return 0;
}
Last edited on
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.
the loop is working and it exits at the 10th try. thank you for info. ill be checking out how to use break; statement
just finished with the last modification. thank you guys really.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int number, tries, notAllowed = 0;
	
	for (tries = 0; tries < 10; tries++)
	{
		cout << "Enter a number except " << notAllowed << ": ";
		cin >> number;
		
			if (number == notAllowed)
			{	
			cout << "You cannot do that!";
			exit(0);
			}
		notAllowed++;
	}
	
	cout << "okay im tired now";
	
	return 0;
}
Topic archived. No new replies allowed.