function logic errors

I know I have some logic errors, but I can't seem to find them. My exit number (-1) isn't working right, so thats one of them. Plus my system clear isn't working right. Any ideas?

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
#include <iostream>
#include <iomanip>

using namespace std;

int menu();
double cube(double);
double rectangle(double);
double sphere(double);
double cylinder(double);

int main() {
   int choice, volume;
   
   do{
      choice = menu();
      
   if ((choice < 1) || (choice > 5)){
      cout << "Invalide" << endl;
   }
   else{
      switch (choice){
         case 1:
            system("clear");
            volume = cube(volume);
            cout << volume << endl;
            break;
            
         case 2:
            system("clear");
            volume = rectangle(volume);
            cout << volume << endl;
            break;
            
         case 3:
            system("clear");
            volume = sphere(volume);
            cout << volume << endl;
            break;
            
         case 4:
            system("clear");
            //cylinder(volume);
            volume = cylinder(volume);
            cout << volume << endl;
            break;
            
         case 5:
            system("clear");
            exit(0);
            break;
      }
   }
   
}while (choice != 5);


   return 0;
}


//Functions

int menu(){
   system("clear");
   int choice;
   
      cout << "Choose a shape to find it's Volume" << endl;
      cout << "Cube(1)" << endl << "Rectangle(2)" << endl
      << "Shpere(3)" << endl << "Cylinder(4)" << endl << "Enter (5) to exit" << endl;
      cin >> choice;
      
      return choice;
      }


double cube(double cubeVolume){
   double length;
   
   cout << "Enter length: " << endl;
   cin >> length;
   
   cubeVolume = length * length * length;
   
   cout << "Cube Volume: ";
   
   return cubeVolume;
}


double rectangle(double rectangleVolume){
   double length, width, height;
   
   cout << "Enter length: " << endl;
   cin >> length;
   cout << "Enter width: " << endl;
   cin >> width;
   cout << "Enter height: " << endl;
   cin >> height;
   
   rectangleVolume = length * width * height;
   
   cout << "Rectangle Volume: ";
   
   return rectangleVolume;
}


double sphere(double sphereVolume){
   double radius;
   
   cout << "Enter radius: " << endl;
   cin >> radius;
   
   sphereVolume = (4/3) * 3.1459 * radius * radius * radius;
   
   cout << "Sphere Volume: ";
   
   return sphereVolume;
}


double cylinder(double cylinderVolume){
   double radius, height;
   
   cout << "Enter radius: " << endl;
   cin >> radius;
   cout << "Enter height: " << endl;
   cin >> height;
   
   cylinderVolume = 3.1459 * radius * radius * height;
   
   cout << "Cylinder Volume: ";
   
   return cylinderVolume;
}
Last edited on
what OS are you using?
I'm on a mac, so OS X I think
Your indentation is off in main() starting at line 18.

You properly validate 'choice' to between 1 and 5, then switch on the results. You never tell the user to enter -1 to exit. I haven't run your program, but it looks like it will exit if you enter -1.

Your functions don't need arguments -- you never use them.

Since your functions do everything, there is no reason to put printing the result outside the function either. The functions should return void and print their results along with everything else.

Your formula for area of a sphere is wrong.

Hope this helps.
I tell the user to enter -1 to exit in my menu function for case 5. I don't think I have any arguments in my functions… what are they?
My debugger says somethings wrong with line 71, but I'm not sure what.
Last edited on
bump
still something wrong with line 71…. I'm using Xcode and it makes a breakpoint there for some reason.
You sure you didn't put the breakpoint there.
Nothing worng with line 71.

1
2
3
4
5
6
7
8
9
10
11
12
double cube(double cubeVolume){ /// the double cubeVolume here is an argument
   double length;
   
   cout << "Enter length: " << endl;
   cin >> length;
   
   cubeVolume = length * length * length;
   
   cout << "Cube Volume: ";
   
   return cubeVolume;
}


Your system("clear") in menu() clears the screen immediately after you show the volume so the user doesn't actually see it.

You could remove the system("clear") in menu() or add a system("pause") after the volume is displayed.
Topic archived. No new replies allowed.