please help, Do-While

Hello, how can I use a do while loop to continue running the program, 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
105
106
107
108
109
110
111
  #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;
}
First, figure out what you want to put in the loop; in this case what do you want to repeat? Then try to figure out under what conditions you should keep repeating.
ok lets see, I would want

1
2
3
4
5
6
{
cout << "Please enter two numbers to calculate " << setprecision(5) << "\nNumber 1 " <<endl;
cin >> num1;
cout <<setprecision(5)<< "\nNumber 2 " <<endl;
cin >> num2;
}


to repeat again, so Im guessing this?

1
2
3
4
5
6
7
8
{
do
cout << "Please enter two numbers to calculate " << setprecision(5) << "\nNumber 1 " <<endl;
cin >> num1;
cout <<setprecision(5)<< "\nNumber 2 " <<endl;
cin >> num2;
while
}


use this:
1
2
3
4
5
6
7
8
do
{
cout << "Please enter two numbers to calculate " << setprecision(5) << "\nNumber 1 " <<endl;
cin >> num1;
cout <<setprecision(5)<< "\nNumber 2 " <<endl;
cin >> num2;

} while (<put in here your conditions to continue the loop>);

Note the semicolon at the end of a Do-While loop

Hope that help you :)
Last edited on
Topic archived. No new replies allowed.