Continue Until Choosing to Exit

Hello, Im having trouble getting the program to continue running until the user chooses to exit. I have managed to create an exit statement, however it doesn't give me the option to continue after the program gives you an answer.
this is what I have so far,


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
  #include <iostream>
#include<iomanip>

using namespace std;

void Values(double &num1, double &num2)
{
cout << "Please enter two numbers to calculate; " << setprecision(5) << "\nNumber 1 " <<endl;
cin >> num1;
cout <<setprecision(5)<< "\nNumber 2 " <<endl;
cin >> num2;
}





void Addition(double &num1, double &num2, double &Result)	
{
Result = num1 + num2;
}

void Subtraction(double &num1, double &num2, double &Result)	
{
Result = num1 - num2;
}

void Multiplication(double &num1, double &num2, double &Result)	
{
Result= num1 * num2;
}

void Division(double &num1, double &num2, double &Result)		
{
	if(num2 != 0) 
		{
			setprecision(5);
			Result = num1 / num2;
		}
	else
		cout << "Please enter a number other than 0 as denominator"<<endl<<endl;
		
}

 



int main()
{
char choice;
double num1,num2, Result;

Values(num1, num2);

cout << endl << endl << "Table of Operations" << endl;
cout << "A - Addition" << endl;
cout << "S - Subtraction" << endl;
cout << "M - Multiplication" << endl;
cout << "D - Division" << endl;
cout << "E - Exit" <<endl<<endl;
cout << "Enter choice for calculation: " ;
cin >> choice;

switch(choice)	 // Switch function allows user to choose the calculation to execute
{
   case 'A':
   case 'a':
      Addition(num1, num2, Result);
      break;
   case 'S':
   case 's':
      Subtraction(num1, num2, Result);
      break;
   case 'M':
   case 'm':
      Multiplication(num1, num2, Result);
      break;
   case 'D':
   case 'd':
      Division(num1, num2, Result);
      break;
   default:
     case 'E':
     case'e':
          cout << "E - Exit Program" << endl << endl;
          exit(0); 
          
     
          cout << "Please enter a valid menu choice from above: ";
          cin >> choice;
          break;
        
      
}

{
      cout << "result is " << Result <<endl;
      }


system("PAUSE");
return 0;
}
The general way is to use a do-while or while loop. For example

while ( true )
{
switch(choice) // Switch function allows user to choose the calculation to execute
{
case 'A':
case 'a':
Addition(num1, num2, Result);
break;
case 'S':
case 's':
Subtraction(num1, num2, Result);
break;
case 'M':
case 'm':
Multiplication(num1, num2, Result);
break;
case 'D':
case 'd':
Division(num1, num2, Result);
break;
default:
case 'E':
case'e':
cout << "E - Exit Program" << endl << endl;
exit(0);


cout << "Please enter a valid menu choice from above: ";
cin >> choice;
break;


}

Last edited on
Thanks, I know I'm suppose to use the do-while, but Im not sure how to add it since i have this,
1
2
3
4
5
6
if(num2 != 0) 
		{
			setprecision(5);
			Result = num1 / num2;
		}
	else
Topic archived. No new replies allowed.