end program from function

Apr 25, 2019 at 12:42pm
I decided just to end the game, If they get it correct, they can move on to the next turn. If they do not get it correct after the second attempt, the game is over. I'm not sure how to do that from a function
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
27
int obs3()  //function obstruction 3
{
	int choice, tries{ 1 };
	cout << " What object do you use to read \n"
		<< " data typed at the keyboard ?\n"
		<< " 1. ( cin )\n"
		<< " 2. ( print )\n"
		<< " 3. ( cout )\n"
		<< " 4. ( enter )\n"
		<< " Enter 1, 2, 3 or 4 \n";
	cin >> choice;

	while (choice != 1)
	{
		cout << " Try again\n";
		cin >> choice; 
		if (tries = 3)
			cout << " Sorry; you failed to overcome the obstacle\n";
		return 0;
		tries++;
		
		return 0;
		}
    cout << " Thats correct\n";
	return 0;
}
  
Last edited on Apr 25, 2019 at 6:09pm
Apr 25, 2019 at 3:03pm
You could put that code in a function that's called from main(). The function might return true/false depending on whether the user wants to play again or not.
Apr 25, 2019 at 3:52pm
does this work in a way that you wanted
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "pch.h"
#include <iostream>
using namespace std;
int obs3()  //function obsruction 3
{
	int choice, tries = 0;
	cout << " What object do you use to read \n"
		<< " data typed at the keyboard ?\n"
		<< " 1. ( cin )\n"
		<< " 2. ( print )\n"
		<< " 3. ( cout )\n"
		<< " 4. ( enter )\n"
		<< " Enter 1, 2, 3 or 4 \n";
	cin >> choice;

	int Choice = 0;
	bool BOOL;

	while (BOOL = true)
	{
		
		std::cout << "try again(1)Yes-(2)No\n ";
		std::cin >> Choice;
		if (Choice == 1)
		{
			BOOL = true;
			++tries;
		}
		else if (Choice == 2)
		{
			break;
		}
		
		if (tries == 3)
		{
			cout << " Sorry; you failed to overcome the obstacle\n";
			break;
		}
	}
}

Last edited on Apr 25, 2019 at 4:07pm
Apr 25, 2019 at 6:03pm
thanks but that does pretty much what I have. I decided just to end the game, If they get it correct, they can move on to the next turn. If they do not get it correct after the second attempt, the game is over. Thanks again
Apr 25, 2019 at 6:26pm
I did it used;

system("pause");
exit (0);
Topic archived. No new replies allowed.