A problem with my code

Hello.

I'm having a problem with a program I'm trying to create. I just got into C++ recently because of a school project, and I can't continue with the project until I fix this. I've tried googling my problem but it didn't help.

The problem is that, when I run the program and I for example select Cube (you will see what I'm talking about from the code), and select for example Volume, when I type in the values and when it writes out the result, it asks for the values again in the same line for some reason.

Another thing is that, for example when I select Sphere and for example Surface Area, it asks for values with aren't even used in the function, but for values which are used in the first one.

As much as I can see, it appears that a function isn't closed, but I can't seem to notice anything.

Hopefully someone can help me out.

Thanks in advance.

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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  #include <iostream>
using namespace std;

int main()

{

    double i, o, k, l, r, s, n, m, q, X, Y, Z, H, J, P, B, G, A, C, U, XX, XXX, YY, YYY, AA, CC, SA, SSA, SAA, SSAA, SSSAA, SSSAAA, V, VO, VVO, VVOO, VVVOO;

    cout << "1) Area and the volume of a cuboid"<<endl;
    cout << "2) Area and the volume of a cube"<<endl;
    cout << "3) Area and the volume of a sphere"<<endl;
    cout << "4) Area and the volume of a cylinder"<<endl;
    cout << "5) Area and the volume of a cone"<<endl;
    cin >> i;

 if (i = 1)

 {

    cout << "1) Calculate the area."<<endl;
    cout << "2) Calculate the volume."<<endl;
    cin >> o;

 }

        if (o = 1)

        {

        cout << "Insert the value of side A";
        cin >> X;

        cout << "Insert the value of side B";
        cin >> Y;

        cout << "Insert the value of side C";
        cin >> Z;



        SA = 2 * ((X * Y) + (Y * Z) + (Z * X));

        cout <<"The surface area is: "<<SA;

        }

        if (o = 2)

        {

        cout << "Insert the value of side A";
        cin >> H;

        cout << "Insert the value of side B";
        cin >> J;

        cout << "Insert the value of side C";
        cin >> P;


        V = H * J * P;

        cout <<"The volume is: "<<V;

        }


 if (i = 2)

 {

    cout << "1) Calculate the area."<<endl;
    cout << "2) Calculate the volume."<<endl;
    cin >> k;

 }

        if (k = 1)

        {

        cout << "Insert the value of side A"<<endl;
        cin >> B;

        SSA = 6*B*B;

        cout <<"The surface area is: "<<SSA;

        }

        if (k = 2)

        {

        cout << "Insert the value of side A"<<endl;
        cin >> G;

        VO = G*G*G;

        cout << "The volume is; "<<VO;

        }

 if (i = 3)

 {

     cout << "1) Calculate the area."<<endl;
     cout << "2) Calculate the volume."<<endl;
     cin >> l;

 }

        if (l = 1)

        {

        cout << "Insert the radius of the sphere"<<endl;
        cin >> r;

        SSAA = 4*3.14*r*r;

        cout << "The area of the sphere is: "<<SSAA;

        }

        if (l = 2)

        {

        cout << "Insert the radius of the sphere"<<endl;
        cin >> U;


        VVO = (4*3.14*U*U*U)/3;

        cout << "The volume of the sphere is; "<<V;

        }


 if (i = 4)

 {

     cout << "1) Calculate the area."<<endl;
     cout << "2) Calculate the volume."<<endl;
     cin >> n;

 }

        if (n = 1)

        {

        cout << "Insert the radius of the cylinder."<<endl;
        cin >> A;

        cout << "Insert the height of the cylinder."<<endl;
        cin >> C;

        SSSAA = 2 * (3.14 * A * A) + 2 * (3.14 * A * C);

        cout << "The surface area of the cylinder is; "<<SSSAA;

        }

        if (n = 2)

        {

        cout << "Insert the radius of the cylinder."<<endl;
        cin >> AA;

        cout << "Insert the height of the cylinder."<<endl;
        cin >> CC;

        VVOO = (3.14 * AA * AA * CC);

        cout << "The volume of the cylinder is; "<<VVOO;

        }

 if (i = 5)

 {

 cout << "1) Calculate the area."<<endl;
 cout << "2) Calculate the volume."<<endl;
 cin >> m;

 }

        if (q = 1)

        {

        cout << "Insert the length of the side"<<endl;
        cin >> XX;

        cout << "Insert the length of the radius"<<endl;
        cin >> YY;

        SSSAAA = (3.14 * YY * XX) + (3.14 * YY * YY);

        cout << "The surface area of the cone is; "<<SSSAAA;

        }

        if (q = 2)

        {

        cout << "Insert the height"<<endl;
        cin >> XXX;

        cout << "Insert the length of the radius"<<endl;
        cin >> YYY;

        VVVOO = (3.14 * YYY * YYY * XXX) / 3;

        cout << "The volume of the cone is; "<<VVVOO;

        }

   
   return 0;


   }




= is assignment and == is to test equality. Change your = to == in your if checks and see how your programs run then.
The program doesn't work at all if I substitute = with ==, since it automatically tries to calculate the values without any pre given values.

Thanks a bunch for the reply though.

The same problem still persists.

Here is a screenshot:

http://i.imgur.com/AAks5Qp.png

As you can see, even though I selected sphere, it asks for cuboid values, and even after it calculated everything, it automatically asks for the next values.
You need to rearrange the if blocks along with the == changes. Next time you are testing something put the constant on the left and the variable on the right like: if (1 == i) . The reason is if you made a mistake and used = instead of == it will be a compiler error.
Look at how I rearranged the if check blocks:
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
166
167
168
169
#include <iostream>
using namespace std;

int main() {

    double i, o, k, l, r, s, n, m, q, 
           X, Y, Z, H, J, P, B, G, A, 
           C, U, XX, XXX, YY, YYY, AA, 
           CC, SA, SSA, SAA, SSAA, SSSAA, 
           SSSAAA, V, VO, VVO, VVOO, VVVOO;

    cout << "1) Area and the volume of a cuboid"<<endl;
    cout << "2) Area and the volume of a cube"<<endl;
    cout << "3) Area and the volume of a sphere"<<endl;
    cout << "4) Area and the volume of a cylinder"<<endl;
    cout << "5) Area and the volume of a cone"<<endl;
    cin >> i;

    if (1 == i) {
       cout << "1) Calculate the area."<<endl;
       cout << "2) Calculate the volume."<<endl;
       cin >> o;
       if (1 == o) {
           cout << "Insert the value of side A";
           cin >> X;
           cout << "Insert the value of side B";
           cin >> Y;
           cout << "Insert the value of side C";
           cin >> Z;
   
           SA = 2 * ((X * Y) + (Y * Z) + (Z * X));
   
           cout <<"The surface area is: "<<SA << endl;
   
        }
        else  if (2 == o) {
           cout << "Insert the value of side A";
           cin >> H;
           cout << "Insert the value of side B";
           cin >> J;
           cout << "Insert the value of side C";
           cin >> P;
   
           V = H * J * P;
           cout <<"The volume is: "<<V << endl;
   
        } else {
           cout << "Invalid value entered: " << o << endl;
        }
    } else if (2 == i) {
       cout << "1) Calculate the area."<<endl;
       cout << "2) Calculate the volume."<<endl;
       cin >> k;
   
       if (1 == k) {
   
           cout << "Insert the value of side A"<<endl;
           cin >> B;
   
           SSA = 6*B*B;
   
           cout <<"The surface area is: "<<SSA << endl;
   
        } else if (2 == k) {
   
           cout << "Insert the value of side A"<<endl;
           cin >> G;
   
           VO = G*G*G;
   
           cout << "The volume is; "<<VO << endl;
   
        } else {
           cout << "Invalid value entered: " << k << endl;
        }
   } else if(3 == i) {
   
        cout << "1) Calculate the area."<<endl;
        cout << "2) Calculate the volume."<<endl;
        cin >> l;

        if (1 == l) {
            cout << "Insert the radius of the sphere"<<endl;
            cin >> r;
    
            SSAA = 4*3.14*r*r;

            cout << "The area of the sphere is: "<<SSAA << endl;

        } else if(2 == l) {

            cout << "Insert the radius of the sphere"<<endl;
            cin >> U;

            VVO = (4*3.14*U*U*U)/3;

            cout << "The volume of the sphere is: "<<VVO << endl;

        } else { 
            cout << "Invalid value entered: " << l << endl;
      }
   } else if (4 == i) {

      cout << "1) Calculate the area."<<endl;
      cout << "2) Calculate the volume."<<endl;
      cin >> n;
 
     if (1 == n) {
        cout << "Insert the radius of the cylinder."<<endl;
        cin >> A;

        cout << "Insert the height of the cylinder."<<endl;
        cin >> C;

        SSSAA = 2 * (3.14 * A * A) + 2 * (3.14 * A * C);

        cout << "The surface area of the cylinder is; "<<SSSAA << endl;

     } else if (2 == n) {

        cout << "Insert the radius of the cylinder."<<endl;
        cin >> AA;

        cout << "Insert the height of the cylinder."<<endl;
        cin >> CC;

        VVOO = (3.14 * AA * AA * CC);

        cout << "The volume of the cylinder is; "<<VVOO << endl;

     } else {
        cout << "Invalid value entered: " << n << endl;
     }
  } else if (5 == i) {
     cout << "1) Calculate the area."<<endl;
     cout << "2) Calculate the volume."<<endl;
     cin >> m;

     if (1 == q) {
        cout << "Insert the length of the side"<<endl;
        cin >> XX;

        cout << "Insert the length of the radius"<<endl;
        cin >> YY;

        SSSAAA = (3.14 * YY * XX) + (3.14 * YY * YY);

        cout << "The surface area of the cone is; "<<SSSAAA << endl;

     } else if (2 == q) {

        cout << "Insert the height"<<endl;
        cin >> XXX;

        cout << "Insert the length of the radius"<<endl;
        cin >> YYY;

        VVVOO = (3.14 * YYY * YYY * XXX) / 3;

        cout << "The volume of the cone is; "<<VVVOO << endl;

     } else {
          cout << "Invalid value entered: " << q << endl; 
     }
   } else {
     cout << "Invalid value entered: " << i << endl;
   }
   return 0;
}
Thanks a lot man!

The code you sent works and pointed me to the right direction. The only thing missing were the else if statements, and of course the mistake you pointed out.

I can finally finish the school project now.

Thanks again man.
Topic archived. No new replies allowed.