repeating loop/function

For my program it keeps repeating the same function instead of going back to the main switch statement. after I choose which function I want to run and it gives me my answer, it will ask the same question again. How do I set the loop/program to go back to the main switch statement or main menu?


here is my program: (sorry its long)


#include <iostream>
#include <cmath>
using namespace std;

void MinMax (int x, int y, int z, int &min, int &max);
void Modulus (int m, int l, int &quotient, int &remainder);
int const ARRAY_SIZE =7;
double getTaylor(double x, int n, double error);
int error;
void Array();
int getInput();
int main()
{
bool check = true;
int choice;

double a, sum= 1, tmp;
int n, i, j;
int x, y, z, min, max;
int m, l;
int quotient, remainder;

choice= getInput();

do {

switch (choice) {
case 1:
cout << "Please enter three integers x, y, z: " << endl;
cin >> x >> y >> z;
MinMax (x, y, z, min, max);


cout << "Min is " << min << "Max is" << max << endl;
break;
case 2:
cout << "Please enter two integers:";
cin >> m >> l;
Modulus(m, l, quotient, remainder);
// call your function
break;
case 3:
cout << "Enter the values for x and n:" << endl;
cin >> a >> n;
cout << endl;
getTaylor (a, n, error);


cout << endl<< "Taylor series estimation is " << sum << "with an error of "<< error << endl;
cout << endl;
break;
case 4:
Array();
break;
case 5:
cout << "End of Program.\n";
check = false;
break;
default:


cout << "You have selected the" << choice << " option /n";
break;
}

} while (check);

return 0;
}
// ******************* END OF MAIN **************************
int getInput()
{

int choice;

cout << endl
<< " 1. MinMax \n"
<< " 2. Modulus \n"
<< " 3. Taylor Series\n"
<< " 4. Array IO \n"
<< " 5. Quit "
<< endl
<< endl
<< " Enter an operation number: ";
cin >> choice;
return choice;
}

void MinMax (int x, int y, int z, int &min, int &max)
{

if (x > y) {
x= max;
y= min;
} else {
y = max;
x = min;
}

if (z < min) {
z = min;
}
if ( z > max) {
z = max;
}

}

void Modulus (int m, int l, int &quotient, int &remainder)
{

quotient = m/l;
remainder = m%l;
cout << "Quotient is " << quotient << endl;
cout << "Remainder " << remainder << endl;


}

double getTaylor(double x, int n, double error)
{
int j, i;
double tmp, sum;
for (i=1; i <= n; i++) {
tmp=x;

for (j=1; j<1; j++)
tmp*=x;
for (j=1; j<= i; j++)
tmp/= j;
sum +=tmp;
}

error = fabs(exp(x) - sum);
return sum;
}


void Array()
{
int i;
char array[ARRAY_SIZE];

cout << "Please enter 7 digits separated by spaces:" << endl;

for (i = 0; i < ARRAY_SIZE; i++)
cin >> array[i];

for(i = 0; i < ARRAY_SIZE; i++)
cout << array[i] << endl;


cout << "Reversed =";
for (i = ARRAY_SIZE - 1; i >=0; i --) {
cout << array[i];
}


}
how do I get my main menu inside the loop
Remember to use code tags!

First off, you need to initialize sum on line 124.

Second, the below code will repeat the loop until the input is 5.

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
#include <iostream>
#include <cmath>
using namespace std;

void MinMax(int x, int y, int z, int &min, int &max);
void Modulus(int m, int l, int &quotient, int &remainder);
int const ARRAY_SIZE = 7;
double getTaylor(double x, int n, double error);
int error;
void Array();
int getInput();
int main()
{
	bool check = true;
	int choice;

	double a, sum = 1, tmp;
	int n, i, j;
	int x, y, z, min, max;
	int m, l;
	int quotient, remainder;

	choice = getInput();

	do {

		switch (choice) {
		case 1:
			cout << "Please enter three integers x, y, z: " << endl;
			cin >> x >> y >> z;
			MinMax(x, y, z, min, max);


			cout << "Min is " << min << "Max is" << max << endl;
			break;
		case 2:
			cout << "Please enter two integers:";
			cin >> m >> l;
			Modulus(m, l, quotient, remainder);
			// call your function
			break;
		case 3:
			cout << "Enter the values for x and n:" << endl;
			cin >> a >> n;
			cout << endl;
			getTaylor(a, n, error);


			cout << endl << "Taylor series estimation is " << sum << "with an error of " << error << endl;
			cout << endl;
			break;
		case 4:
			Array();
			break;
		case 5:
			cout << "End of Program.\n";
			check = false;
			break;
		default:
			cout << "You have selected the" << choice << " option /n";
			break;
		}
		if (check){
			choice = getInput();
		}
	} while (check);

	return 0;
}
// ******************* END OF MAIN **************************
int getInput()
{

	int choice;

	cout << endl
		<< " 1. MinMax \n"
		<< " 2. Modulus \n"
		<< " 3. Taylor Series\n"
		<< " 4. Array IO \n"
		<< " 5. Quit "
		<< endl
		<< endl
		<< " Enter an operation number: ";
	cin >> choice;
	return choice;
}

void MinMax(int x, int y, int z, int &min, int &max)
{

	if (x > y) {
		x = max;
		y = min;
	}
	else {
		y = max;
		x = min;
	}

	if (z < min) {
		z = min;
	}
	if (z > max) {
		z = max;
	}

}

void Modulus(int m, int l, int &quotient, int &remainder)
{

	quotient = m / l;
	remainder = m%l;
	cout << "Quotient is " << quotient << endl;
	cout << "Remainder " << remainder << endl;


}

double getTaylor(double x, int n, double error)
{
	int j, i;
	double tmp, sum;
	for (i = 1; i <= n; i++) {
		tmp = x;

		for (j = 1; j<1; j++)
			tmp *= x;
		for (j = 1; j <= i; j++)
			tmp /= j;
		sum += tmp;
	}

	error = fabs(exp(x) - sum);
	return sum;
}


void Array()
{
	int i;
	char array[ARRAY_SIZE];

	cout << "Please enter 7 digits separated by spaces:" << endl;

	for (i = 0; i < ARRAY_SIZE; i++)
		cin >> array[i];

	for (i = 0; i < ARRAY_SIZE; i++)
		cout << array[i] << endl;


	cout << "Reversed =";
	for (i = ARRAY_SIZE - 1; i >= 0; i--) {
		cout << array[i];
	}


}
Last edited on
thank you so much!

I initialized my sum variable.
Topic archived. No new replies allowed.