I just wrote a program for a homework assignment. I'm quite the beginner to C++ (and programming) and I was hoping someone could review the code, and let me know what's going on with some of my problems.
My program performs 4 calculations and allows users to choose from a menu (using a switch).
Problems: My code only performs one calculation and then it closes. I want the user to be able to repeat calculations if desired after their primary calculation. I have a quit option whenever the user is ready to quit.
Also, I have a sub-menu for circle calculations: the user can calculate circle circumference or area. When I choose area or circumference, the program malfunctions. I can't figure this one out!
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main()
{
//Declaration of variables.
constdouble PI = 3.1414926;
double diameter = 0.0;
double circumference = 0.0;
double length = 0.0;
double width = 0.0;
double radius = 0.0;
double area = 0.0;
double height = 0.0;
double base = 0.0;
//shape_choice lets user decide which type of calculation he wants to perform.
//formula_choice lets user decide which circle calculation he wants to perform.
char shape_choice;
int formula_choice;
//Display menu, prompts user for choice.
cout << "Geometry Calculator:" << endl << endl;
cout << "This program allows you to calculate the area of 4 shapes." << endl << endl;
cout << "Please choose from the following menu and enter your choice." << endl << endl;
cout << "Enter S to calculate the area of a square." << endl << endl;
cout << "Enter R to calculate the area of a rectangle." << endl << endl;
cout << "Enter C to calculate the area or circumference of a circle." << endl << endl;
cout << "Enter T to calculate the area of a triangle." << endl << endl;
cout << "Enter Q to quit the program when finished." << endl << endl;
cout << "Please enter your choice." << endl << endl;
cin >> shape_choice;
switch(shape_choice)
{
case'S':
case's':
cout << "Enter the length of the square's side." << endl;
cin >> length;
//square area formula:
area = pow(length, 2);
cout << "The area of the square is " << area << endl << endl;
break;
case'R':
case'r':
cout << "Enter the width of the rectangle." << endl << endl;
cin >> width;
cout << "Enter the length of the rectangle." << endl << endl;
cin >> length;
//rectangle area formula:
area = length * width;
cout << "The area of the rectangle is " << area << endl << endl;
break;
case'C':
case'c':
cout << "Would you like to calculate the area or circumference of the circle?" << endl << endl;
cout << "Enter 1 to calculate the area." << endl << endl;
cout << "Enter 2 to calculate the circumference." << endl << endl;
cin >> formula_choice;
//Sub-menu to promt user for type of circle calculation: area or circumference.
switch(formula_choice)
{
case'1':
cout << "Area of a circle:" << endl << endl;
cout << "Enter the radius of the circle." << endl << endl;
cout << "Remember, the radius is half of the diameter!" << endl << endl;
cin >> radius;
//circle area formula
area = PI * pow(radius, 2);
cout << "The area of the circle is " << area << endl << endl;
break;
case'2':
cout << "Circumference of a circle:" << endl << endl;
cout << "Enter the diameter of the circle." << endl << endl;
cout << "Remember, the diameter is two times the radius!" << endl << endl;
cin >> diameter;
//circle circumference formula:
circumference = PI * diameter;
cout << "The circumference of the circle is " << circumference << endl << endl;
break;
default:
cout << "That is not a valid selection." << endl << endl;
cout << "Select 1 for area or 2 for circumference." << endl << endl;
break;
}
case'T':
case't':
cout << "Enter the triangle's base." << endl << endl;
cin >> base;
cout << "Enter the triangle's height." << endl << endl;
cin >> height;
//triangle area formula:
area = (0.5) * base * height;
cout << "The area of the triangle is " << area << endl << endl;
//Quit option:
case'Q':
case'q':
cout << "Thank you for using the Geometry Calculator. Goodbye!" << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
default:
cout << "That is not a valid selection." << endl << endl;
cout << "Select a valid shape choice: S, R, C, T, or Q." << endl << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}