problem in exit 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
void main()
{
	int menu;

	do
	{
                [b]//main menu[/b]
		cout<<"Press 1 to create an object of rectangle class.\n";  
		cout<<"Press 2 to create an object of square class.\n";
		cout<<"Press 3 to create an object of ellipse class.\n";
		cout<<"Press 4 to create an object of circle class.\n";
		cout<<"Press -1 to exit.\n";
		cin>>menu;

		if(menu ==1)
		{
			shape * s = new rectangle;
			s->input();
			int temp;
			do
			{
                        [b]//sub menu[/b]
			cout<<"Press 1 to display area of rectangle.\n";
			cout<<"Press 2 to display perimeter of rectangle.\n";
			cout<<"Press 3 to return to main menu.\n";
			cin>>temp;

			if(temp == 1)
			{
				s->calc_area();
			}
			else if(temp == 2)
			{
				s->calc_perm();
			}
			else if(temp == 3)
			{
				exit(1);
			}
			}while(temp!=3);
		}
		else if(menu==2)
		{
			shape * t= new square;
			t->input();
			int temp;
			do
			{
			cout<<"Press 1 to display area of square.\n";
			cout<<"Press 2 to display perimeter of square.\n";
			cout<<"Press 3 to return to main menu.\n";
			cin>>temp;

			if(temp == 1)
			{
				t->calc_area();
			}
			else if(temp == 2)
			{
				t->calc_perm();
			}
			else if(temp == 3)
			{
				exit(1);
			}
			}while(temp!=3);
		}
		else if(menu==3)
		{
			shape *p=new ellipse;
			p->input();
			int temp;
			do
			{
			cout<<"Press 1 to display eccentricity of ellipse.\n";
			cout<<"Press 2 to return to main menu.\n";
			cin>>temp;

			if(temp == 1)
			{
				p->calc_eccentricity();
			}
			else if(temp == 2)
			{
				exit(1);
			}
			}while(temp!=3);
		}
			
		else if(menu==4)
		{
			shape *c=new circle;
			c->input();
			int temp;
			do
			{
			cout<<"Press 1 to display area of circle.\n";
			cout<<"Press 2 to display circumference of circle.\n";
			cout<<"Press 3 to return to main menu.\n";
			cin>>temp;

			if(temp == 1)
			{
				c->calc_area();
			}
			else if(temp == 2)
			{
				c->calc_circum();
			}
			else if(temp == 3)
			{
				exit(1);
			}
			}while(temp!=3);
		}
			
	}while(menu != -1);
}


Guys!
In this code i when i am in the sub-menu and when i press 3 to go to main menu by using exit(1) function or return function, my program gets end and does not go to main menu. But i want it to go to main menu... Someone help me plz.
closed account (9wX36Up4)


1
2
3
4
5
else if(temp == 3)
{
	exit(4);
}
}while(temp==1||temp==2);

||=mentally or
&&=mentally and
http://www.cplusplus.com/reference/clibrary/cstdio/fread/
Last edited on
I am pretty sure exit ends the program and not the loop, the keyword break should break the loop and return to the main menu.

1
2
else if(temp == 3)
    break;
+1 Warnis
closed account (9wX36Up4)
Or u can use
1
2
3
4
5
else if(temp == 3)
{
	return main();
}
}while(temp==1||temp==2);
That would just recur, making the user quit from multiple menus at best.
closed account (9wX36Up4)
yes u r right but it s useful too.
Topic archived. No new replies allowed.