Creating loop for functions-

Hello out there;
Thank you for taking the time to help.
I need to figure out how to create a loop for my functions.
1st needed to create a menu with choices
a- calculate mpg
b- calculate inflation rate
c- select the largest number of 2 parameters
d- select the largest number of 3 parameters
x- exit
This has been done, but I can only porperly access choices: A & X
I can access B, but it uses the calculation of "A", and
C & D are not accessible at all.
Plus, Choices A-D need to be made so the user can repeat the calculations as many times desired so.
Hence my next problem, not being able to properly place and create the loop for each section.
I would greatly appreciate the help.
Thnx in advance


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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

// Prototypes
void errorMsg();
char UpCase(char);
void GetData(double&, double&);
void CalculateMPG(double, double, double&);
void DisplayMPG(double);

void GetPrices(double&, double&);
void CalculateInflationRate(double, double, double&);
void DisplayInflationRate(double);

void GetParameters(double&, double&);
void ReturnLargestValueI(double, double, double&);
void DisplayLargestValueI(double);

void GetValues(double&, double&, double&);
void ReturnLargestValueII(double, double, double, double&);
void DisplayLargestValueII(double);



int main()
{

	bool userNotDone = true; 
	char menuChoice;
	double fuel, 
		distance,
		mpg;
	double price1,
		price2,
		ir;
	double parameter1,
		parameter2,
		largestValue1;
	double value1,
		value2,
		value3,
		largestValue2;


	while (userNotDone)
	{
		system("cls");
		cout << "\n\n\n\n\n\n\n\n\n";
			cout << "\t\t\t\tMenu" << endl;
			cout << "\t\t\tA. Calculate miles per gallon" << endl;
			cout << "\t\t\tB. Calculate rate of Inflation" << endl;
			cout << "\t\t\tC. Identify the largest of two parameters" << endl;
			cout << "\t\t\tD. Identify the largest of three parameters" << endl;
			cout << "\t\t\tX. Exit Program" << endl;
			cout << "\t\t\tEnter Menu Choice ==> ";
			cin >> menuChoice;


		switch (UpCase (menuChoice))
		{
		case 'A': 
			GetData(fuel,distance);
			CalculateMPG(fuel,distance,mpg);//sned in fuel, distance and get mpg back
			DisplayMPG(mpg);
			break;
		case 'B':
			GetData(price1, price2);
			CalculateInflationRate(price1, price2, ir);
			DisplayInflationRate(ir);
			break;
		case 'C':
			GetParameters(parameter1, parameter2);
			ReturnLargestValueI(parameter1, parameter2, largestValue1);
			DisplayLargestValueI(largestValue1);
			break;
		case 'D':
			GetValues(value1, value2, value3);
			ReturnLargestValueII(value1, value2, largestValue2);
			DisplayLargestValueII(largestValue2);
			break;
		case 'X':
			userNotDone = false;
			system("pause");
			break;
		default:
			errorMsg();
		}
	}

	return 0;

}

void errorMsg()
{
	cout << "\n\n\t\t\tYou must enter A, B, C, D or X" << endl;
	system("pause");
}

char UpCase(char letter)
{
	if(letter >=97 && letter <= 122)
		letter-=32;
	return letter;
}

////////////////////////////////
//// Part I (Secion A)/////
//////////////////////////////

//Input 
void GetData(double& liters, double& miles) // get this number from input
{
	system("cls");
	cout << "Enter the number of liters consumed ==>";
	cin >> liters;
	cout << "Enter the number of miles traveled  ==>";
	cin >> miles;

}
//Process
void CalculateMPG(double liters, double miles, double& mpg)// this is the value (calculation)
{
	mpg = miles / (liters * 0.264179);
}
//Output
void DisplayMPG(double mpg) // pass mpg from above (display)
{
	cout << setprecision(2) << fixed;
	cout <<"Your mileage is " <<mpg << " miles per gallon\n\n\n";
	system("pause");
}

/////////////////////////////////
////Part (Section B)/////
///////////////////////////////
//Input
void GetPrices(double& price1, double& price2)
{
	system("cls");
	cout << "Enter price of item last year ==>";
	cin >> price1;
	cout << "Enter price of item this year ==>";
	cin >> price2;
}
//Process
void CalculateInflationRate(double price1, double price2, double&ir)
{
	ir= ((price2 - price1)/((price1)/100)); 
}
//Output
void DisplayInflationRate(double ir)
{
	cout << setprecision(2) << fixed;
	cout << "Your inflation rate is " <<ir << "\n\n\n";
	system("pause");
}

/////////////////////////////////
////Part III (Section C)/////
///////////////////////////////
//Input
void GetParameters(double& parameter1, double& parameter2)
{
	system("cls");
	cout << "Enter your first parameter  ==>";
	cin >> parameter1;
	cout << "Enter your second parameter ==>";
	cin >> parameter2;
}
//Process
void ReturnLargestValueI(double parameter1, double parameter2, double&largestValue1)
{
	if (parameter1 <= parameter2)
	{
		largestValue1 = parameter2;
	}
	else if (parameter2 <= parameter1)
	{
		largestValue1 = parameter1;
	}
}
//Output
void DisplayLargestValueI(double largestValue1)
{
	cout << "The largest number is " <<largestValue1 << "\n\n\n";
	system("pause");
}

/////////////////////////////////
////Part IV (Section D)/////
///////////////////////////////
//Input
void GetValues(double& value1, double& value2, double& value3)
{
	system("cls");
	cout << "Enter your first parameter  ==>";
	cin >> value1;
	cout << "Enter your second parameter ==>";
	cin >> value2;
	cout << "Enter your third parameter  ==>";
	cin >> value3;
}
//Process
void ReturnLargestValueII(double value1, double value2, double value3, double& largestValue2)
{
	if (value1 <= value2 && value2 <= value3)
	{
		largestValue2 = value3;
	}
	else if (value1 <= value3 && value3 <= value2)
	{
		largestValue2 = value2;
	}
	else if (value2 <= value1 && value1 <= value3)
	{
		largestValue2 = value3;
	}
	else if (value2 <= value3 && value3 <= value1)
	{
		largestValue2 = value1;
	}
	else if (value3 <= value1 && value1 <= value2)
	{
		largestValue2 = value2;
	}
	else if (value3 <= value2 && value2 <= value1)
	{
		largestValue2 = value1;
	}
}
//Output
void DisplayLargestValueII(double largestValue2)
{
	cout << "The largest number is " <<largestValue2 << "\n\n\n";
	system("pause");
}
Last edited on
Hello, if you want the program to repeat the choices i suggest you wrap your switch() in a while loop that continues until a special condition has been met for example:

1
2
3
4
5
6
7
8
9
10
11
bool TrueOrFalse = false;
int i = 1;
while(!TrueOrFalse)
{
   //do something here
   if(i == 2)
   {
      TrueOrFalse = true;
   }
   ++i;
}


EDIT: you can use the same for the calculations
Last edited on
Please format your code with [code] tags. It will make your post more legible.
Topic archived. No new replies allowed.