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.
#include <iostream>
usingnamespace std;
int main()
{
constdouble 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.
#include <iostream>
usingnamespace std;
int main()
{
constdouble 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;
}
int main()
{
constdouble 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".
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.
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".