Geometry Claculator-functions not working

Hello!

I am a beginner to programming and taking a c++ class at my local college as part of a career/major change. Choosing an online class for my first try at this was not my finest moment, but here I am.

My code doesn't seem to work. I have checked online, here on the forums as well as YouTube and can't seem to nail it down. The nest I can figure, my functions aren't returning anything and I'm not sure why...

Any hints or tips? I'd appreciate any help you can give!

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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
 #include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int displayMenu();
double calcAreaCircle( double radius ); 	 
double calcAreaRect( double length, double width ); 	
double calcAreaTriangle( double base, double height ); 

const double Pi = 3.14159;

int main ()
{
    
	int choice;

	double radius, width, length, base, height;
	
	double menu;
    
	menu=displayMenu();
    
    double circArea;

    circArea = calcAreaCircle (radius);
  
    double rectArea;

    rectArea = calcAreaRect (length, width);
    
    double triArea;

    triArea = calcAreaTriangle (base, height);
    
       switch(choice) 
        {        
            case 1:
            {
                cout << "Please enter the Radius:" << endl; 
                cin >> radius;
             }   
                break;

            case 2:
            {
                cout << "Please enter the Length:" << endl; 
                cin >> length; 
                cout << endl; 
 
                cout << "Please enter the width:" << endl; 
                cin >> width; 
                cout << endl; 
              } 	
            break;

            case 3: 
            {
                cout << "Please enter the Base:" << endl; 
                cin >> base; 
                cout << endl; 
 
                cout << "Please enter the Height:" << endl; 
                cin >> height; 
                cout << endl; 
  
              } 
             
            break;

            case 4:
            cout<< "Bye! "<<endl;
            system ("pause");	 
            break; 
   
       } 
     
 }  
 
 int displayMenu()
{
    int choice;	

    cout << "Geometry Calculator"<<endl; 
    cout << "1. Calculate the Area of a Circle"<<endl; 
    cout << "2. Calculate the Area of a Rectangle"<<endl; 
    cout << "3. Calculate the Area of a Triangle"<<endl; 
    cout << "4. Quit"; 
    cout << endl;
    cin >> choice;

    
    while (choice < 1 || choice > 4)
    {
        cout << "Invalid selection. Enter 1, 2, 3, or 4: ";
        cin >> choice;
    }
    
}
    
 return choice (int);
 }



 double calcAreaCircle(double radius)	 
    
 {
     
        double circArea = Pi*radius*radius; 
  
    if (radius > 0)	  
     {	  
      cout << "The area of the circle " << radius << " is: " << area << endl;
	  system ("pause"); 
        return 0; 
     } 
    else	
    { 
        cout << "You did not enter a valid number" << endl; 
        cout << "The program will now restart" << endl; 
        system ("pause"); 
        return circArea;
    } 
    } 
 
 
double calcAreaRect (double width, double length)	
  { 
    double rectArea = length * width
    
    if (length > 0, width > 0)	  
     {	  
        cout << "The Area for the Rectangle is: " << area << endl; 
        system ("pause"); 
        return rectArea; 
     } 
 
    else	
     {	  
        cout << "You did not enter valid numbers." << endl; 
        cout << "The program will now restart." << endl; 
        system ("pause"); 
        return rectArea; 
     } 
} 
 
double calcAreaTriangle (double base, double height )
  {
    double triArea = base * height
        
 
    if (base > 0, height > 0)		
     {	
        triArea = .5 * base * height;
     } 
    else	
     {	  
        cout << "You did not enter a valid number." << endl;	
        cout << "Please retry your entry." << endl; 
        system ("pause"); 
        return triArea; 
   } 

}
Hi,
Please give us some hints.
What is your assignment about?
What is your approach to solve the problem? And which part does your program go wrong (Input/Calculation/Output) etc?
You haven't got enough data for calculation. Before simply the input part should have been done first prior to calculation.

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
double circArea;
    circArea = calcAreaCircle (radius);
  
    double rectArea;
    rectArea = calcAreaRect (length, width);
    
    double triArea;
    triArea = calcAreaTriangle (base, height);
    
       switch(choice) 
        {        
            case 1:
            {
                cout << "Please enter the Radius:" << endl; 
                cin >> radius;
             }   
                break;
            case 2:
            {
                cout << "Please enter the Length:" << endl; 
                cin >> length; 
                cout << endl; 
 
                cout << "Please enter the width:" << endl; 
                cin >> width; 
                cout << endl; 
              } 	
            break;
            case 3: 
            {
                cout << "Please enter the Base:" << endl; 
                cin >> base; 
                cout << endl; 
 
                cout << "Please enter the Height:" << endl; 
                cin >> height; 
                cout << endl; 
  
              } 
             
            break;
            case 4:
            cout<< "Bye! "<<endl;
            system ("pause");	 
            break; 
   
       }


Change it to :
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
       switch(choice) 
        {        
            case 1:
            {
                cout << "Please enter the Radius:" << endl; 
                cin >> radius;
             }   
                break;
            case 2:
            {
                cout << "Please enter the Length:" << endl; 
                cin >> length; 
                cout << endl; 
 
                cout << "Please enter the width:" << endl; 
                cin >> width; 
                cout << endl; 
              } 	
            break;
            case 3: 
            {
                cout << "Please enter the Base:" << endl; 
                cin >> base; 
                cout << endl; 
 
                cout << "Please enter the Height:" << endl; 
                cin >> height; 
                cout << endl; 
  
              } 
             
            break;
            case 4:
            cout<< "Bye! "<<endl;
            system ("pause");	 
            break;    
       }

double circArea;
    circArea = calcAreaCircle (radius);
  
    double rectArea;
    rectArea = calcAreaRect (length, width);
    
    double triArea;
    triArea = calcAreaTriangle (base, height);
Does it help you? :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int displayMenu()
{
    int choice;	
    cout << "Geometry Calculator"<<endl; 
    cout << "1. Calculate the Area of a Circle"<<endl; 
    cout << "2. Calculate the Area of a Rectangle"<<endl; 
    cout << "3. Calculate the Area of a Triangle"<<endl; 
    cout << "4. Quit"; 
    cout << endl;
    cin >> choice;
    
    while (choice < 1 || choice > 4)
    {
        cout << "Invalid selection. Enter 1, 2, 3, or 4: ";
        cin >> choice;
    }
     return choice;   
}


You forgot to return the choice you selected from displayMenu(). That means your main() function has no way to know which choice you picked from displayMenu(). But even when this problem is solved, you are using a wrong variable in the function main() to catch the return value from displayMenu(). So instead of writing this line :
menu = displayMenu();

You write:
choice = displayMenu();
Yes! Thank you so much, you were a huge help. I think part of it was I was staring at things too long :)
Glad it helped :)
Hi,

This doesn't do what you think it does:

if (base > 0, height > 0)

Try this instead:

1
2
3
if (base > 0.0 and height > 0.0) { // and is the same as &&
   // your code
}


I always put digits before and after the decimal place, it reinforces the idea that we are dealing with doubles.

The logic in your functions is a little bit off: you return 0 instead of the valid circle area, but return an invalid circle area when the input is invalid. The code that calls the calculation function should check the value returned - given that 0 means invalid. Better to make sure data is valid before calling any functions.

Always put a default case in a switch statement, it catches bad input. The same if doing an if else if chain, always put an else statement.

The switch statement could call separate function to get and validate the input for various shapes.

Don't bother to calculate answers if you are not sure the input is correct. In other words do things in the appropriate order.

With this:

1
2
3
    double circArea;

    circArea = calcAreaCircle (radius);


consider doing this:

1
2
3
     

    double circArea = calcAreaCircle (radius);


Hope this helps :+)

@closed account 5a8Ym39o6

Is that you VRGaming?
Last edited on
@TheIdeasMan
You can't make false assumptions or make up facts without any proof :)

If I were really VRGaming, I would not know that much about programming. Also, I would post pretty rarely.
@closed account 5a8Ym39o6

Sorry, it was just an innocent question, wasn't trying to state facts or make assumptions. So a simple "No" answer may have been sufficient :+)

However good marks for imagination on the user name and bio - very original :+)
Topic archived. No new replies allowed.