Help with Looping

So I need to write a program with menu, but the menu needs to loop after a certain input is entered. I'm think a do-while loop would work but I am unsure of how to implement it into the quit part of the menu. Any suggestions?


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

int main()
{
	double radius, error;
	char method_choice = 'x';
	char rerun_choice = 'z';


	cout << showpoint << fixed;
	cout << setprecision(4);

	radius = 0;

	cout << "Compute Area of a Circle" << endl << endl;
	cout << "Enter Radius: ";
	cin >> radius;
	cout << "Enter Acceptable Error: ";
	cin >> error;

	cout << "Select a method:" << endl;
	cout << "A.	Blah" << endl;
	cout << "B.	Blah Blah" << endl;
	cout << "C.	Blah Blah Blah" << endl;
	cout << "Enter Choice: ";
	cin >> method_choice;

	method_choice = toupper(method_choice);
	switch (method_choice)
	{
	case 'A':
		//Math Equations
		break;

	case 'B':
		//Math Equations
		break;

	case 'C':
		//Math Equations
		break;
	default:
		cout << "Error" << endl;
	}

	cout << endl << "Quit?" << endl;
	cout << "Y. Yes" << endl;
	cout << "N. No" << endl;
	cout << "Enter Choice: ";
	cin >> rerun_choice;

	rerun_choice = toupper(rerun_choice);
	switch (rerun_choice)
	{
	case 'Y':
		break;

	case 'N':
		break;

	default:
		cout << "Invalid choice" << endl;
	}

}
Last edited on
Please edit your post and use code tags - http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
8
bool run = true,
while(run) // runs while run ==  true
{
    if(user wants to quit)
    {
         run = false;
    }
}
How can I add my switch to ask if the user wants to rerun or quit though?
You have switch, you need to add loop:

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
bool rerun = true;
while (rerun) {
    radius = 0;
    cout << "Compute Area of a Circle" << endl << endl;
    // ... code ...
    cout << endl << "Quit?" << endl;
    cout << "Y. Yes" << endl;
    cout << "N. No" << endl;
    cout << "Enter Choice: ";
    cin >> rerun_choice;
    rerun_choice = toupper(rerun_choice);
    switch (rerun_choice)
    {
        case 'Y':
             rerun = false;
             break;
        case 'N':
             rerun = true;
	     break;
	default:
	     cout << "Invalid choice" << endl;
             rerun = false;
             break;
    }
}


Or you can do it like this:

1
2
3
4
5
6
7
8
9
10
11
12
char rerun_choice = 'N';
while (rerun_choice == 'N') {
    radius = 0;
    cout << "Compute Area of a Circle" << endl << endl;
    // ... code ...
    cout << endl << "Quit?" << endl;
    cout << "Y. Yes" << endl;
    cout << "N. No" << endl;
    cout << "Enter Choice: ";
    cin >> rerun_choice;
    rerun_choice = toupper(rerun_choice);
}
Topic archived. No new replies allowed.