Ask the user if they want to rerun the program?

The problem is, we have to modify the code to ask the user if they want to run the program again. I already have a do-while loop set up, but I'm not sure where to put my variable as every time it says that I'm using an uninitialized variable.

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

int main()
{
    const double PI = 3.14159;

    int    choice;         // User's shape choice
    double radius,         // Circle radius
           length,         // Rectangle length
           width,          // Rectangle width
           base,           // Triangle base
           height,         // Triangle height
           area;           // Area of the selected shape
	char   input;		   // User's rerun choice


	// Display selections and request user input
	cout << "Geometry Calculator\n\n";
	cout << "1. Calculate the area of a Circle\n";
	cout << "2. Calculate the area of a Rectangle\n";
	cout << "3. Calculate the area of a Triangle\n";
	cout << "4. Quit\n\n";
	cout << "Enter your choice (1-4): ";
	cin >> choice;

	// Calculate and display the area of the selected shape
	do
	{
		char input;
		switch (choice)
		{
		case 1:  // Area of a circle
			cout << "\nEnter the circle's radius: ";
			cin >> radius;

			if (radius < 0)
				cout << "\nThe radius can not be less than zero.\n";
			else
			{
				area = PI * radius * radius;
				cout << "\nThe area is " << area << endl;

				break;
			}

		case 2:  // Area of a rectangle
			cout << "\nEnter the rectangle's length: ";
			cin >> length;
			cout << "Enter the rectangle's width: ";
			cin >> width;

			if (length < 0 || width < 0)
				cout << "\nOnly enter positive values for length and width.\n";
			else
			{
				area = length * width;
				cout << "\nThe area is " << area << endl;
			}
			break;

		case 3:  // Area of a triangle
			cout << "Enter the length of the base: ";
			cin >> base;
			cout << "Enter the triangle's height: ";
			cin >> height;

			if (base < 0 || height < 0)
				cout << "\nOnly enter positive values for base and height.\n";
			else
			{
				area = base * height * 0.5;
				cout << "\nThe area is " << area << endl;
			}
			break;

		case 4:	 cout << "\nBye!\n";
			break;

		default:  cout << "\nYou may only enter 1, 2, 3, or 4.\n";
		}
	} while (input == 'y');
	return 0;
}
So i figured that out, but now I'm not sure how to change it so if the user inputs choice 4 it doesn't show the "Do you want to run it again?". 4 is the quit the program option so i dont want it to ask if they want to rerun it.

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

int main()
{
    const double PI = 3.14159;

    int    choice;         // User's shape choice
    double radius,         // Circle radius
           length,         // Rectangle length
           width,          // Rectangle width
           base,           // Triangle base
           height,         // Triangle height
           area;           // Area of the selected shape
	char   rerun;		   // User's rerun choice

	do
	{

		// Display selections and request user input
		cout << "Geometry Calculator\n\n";
		cout << "1. Calculate the area of a Circle\n";
		cout << "2. Calculate the area of a Rectangle\n";
		cout << "3. Calculate the area of a Triangle\n";
		cout << "4. Quit\n\n";
		cout << "Enter your choice (1-4): ";
		cin >> choice;

		// Calculate and display the area of the selected shape
		switch (choice)
		{
		case 1:  // Area of a circle
			cout << "\nEnter the circle's radius: ";
			cin >> radius;

			if (radius < 0)
				cout << "\nThe radius can not be less than zero.\n";
			else
			{
				area = PI * radius * radius;
				cout << "\nThe area is " << area << endl;
				break;
			}

		case 2:  // Area of a rectangle
			cout << "\nEnter the rectangle's length: ";
			cin >> length;
			cout << "Enter the rectangle's width: ";
			cin >> width;

			if (length < 0 || width < 0)
				cout << "\nOnly enter positive values for length and width.\n";
			else
			{
				area = length * width;
				cout << "\nThe area is " << area << endl;
			}
			break;

		case 3:  // Area of a triangle
			cout << "Enter the length of the base: ";
			cin >> base;
			cout << "Enter the triangle's height: ";
			cin >> height;

			if (base < 0 || height < 0)
				cout << "\nOnly enter positive values for base and height.\n";
			else
			{
				area = base * height * 0.5;
				cout << "\nThe area is " << area << endl;
			}
			break;

		case 4:	 cout << "\nBye!\n";
			break;

		default:  cout << "\nYou may only enter 1, 2, 3, or 4.\n";
		}
		cout << "\nDo you want to run that again? y/n: ";
		cin >> rerun;
	} while (rerun == 'y' || rerun == 'Y');
	return 0;
}
Last edited on
Hello morganniie,

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    const double PI = 3.14159;

    int    choice{};      // User's shape choice  // <--- ALWAYS initialize all your variables.
    double radius{},      // Circle radius
        length{},         // Rectangle length
        width{},          // Rectangle width
        base{},           // Triangle base
        height{},         // Triangle height
        area{};           // Area of the selected shape
    char   rerun{};       // <--- Or char rerun{'n'}; 

You could also include the header file "<cctype>" and say
while (std::toupper(rerun) == 'Y' or use tolower and a lower case "y".

Andy
Hello morganniie,

After running your program give this a try for "case 4":
1
2
3
4
5
case 4:
    cout << "\nBye!\n";
    rerun = 'n';
    continue;
    break;

Case 4 should end the program not ask the user if they want another run.

Andy
Yes!! That worked thank you so much. I didn't really understand what you were talking about in the first post, (I'm only in week 4 of Principles of Programming) but that does make sense.
Hello morganniie,

Always initialize your variables. You may be use to seeing int choice = 0;.

The {}s, or uniform initializer, started with the C++ 2011 standards. Empty it will initialize the variable to (0)zero based on the variable's type. In the case of a "double" it would be (0.0).

For "std::string"s they are empty when defined or between the {}s use double quotes around the string and for a "char" use single quotes as I did with "rerun".

Andy
Topic archived. No new replies allowed.