C++ homework help!

Hi everyone,

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!

Here is my code:
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
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    //Declaration of variables.
    const double 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;
}
Any and all help is greatly appreciated!! I understand the basics, but some of these program (algorithm?) mistakes really get me lost...
If you want it to repeat, you need a loop.
Add the top of your loop after line 29.
Add the bottom of you loop after line 124.

case 'c' in your outer switch doesn't have a break statement. It will fall through to case 't'. Add a break statement at line 100.

case 't' doesn't have a break statement. It will fall through to case 'q' and quit.
Add a break statement at line 111.



Thank you so much!

Sometimes I just go blind from reading the same thing over and over again. It's helpful to have someone else look ever it.
Topic archived. No new replies allowed.