Question about if-else

Hi everyone ,

I wanted to write a program that calcule the surface area of cube,Rectangular Prism,Cylinder and sphere.I have coded and there is no structure mistake in the program but the program is in a trouble.The trouble is user select a shape from the list but that's not matter what they select , program calculates only for the cube.

If you copy this program and work you will see the mistake and what I mean.

Could you help me for this stuation ?
Thank you.

Surface Area of Cube :
saoc=6a2
a=lenght of a side of the cube

Surface Area of Rectangular Prism:
saorp=2(x*y+x*z+y*z)
x=width of prism
y=depth of prism
z=heigth of prism
Surface Area of Cylinder :
saocc=2*3.1415*r2+2*3.1415*h
r=radius of the circle
h=height of the cylinder
Surface Area of Sphere :
saos=4*3.1415*k2
k=radius of sphere


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
using namespace std ;

#include<iostream>
#include<cmath>
#include<fstream>

int main ()

{
    double  a,x,y,z,r,h,k,saoc,saorp,saocc,saos ; 
    int n,g,o,p,j,u;
    ofstream dataout ("yenidata.txt") ;
    
    cout<<"Please choose the number that refers to the shape that you want to calculate of surface area "<<endl;
    cout<<"1-Cube"<<endl;
    cout<<"2-Rectangular Prism"<<endl;
    cout<<"3-Cylinder "<<endl;
    cout<<"4-Sphere"<<endl;
    cin>>n;
    cout<<"which unit you will give the values and which unit you want result"<<endl;
           cout<<"1-m2(given) -  cm2(expected)"<<endl;
           cout<<"2-cm2(given) - m2(expected)"<<endl;
           cin>>u ;
    if((n=1) && (u=1))
    
          { 
           cout<<"Please enter a side lenght of the cube"<<endl;
           cin>>a;
    
                  saoc=6*pow(a,2);
                  saoc=saoc*100 ;
                  cout<<"Surface Area of the Cube="<<saoc<<"cm2"<<endl;
                  
                  }
                  
           if ( (n=1) && (u=2) )
           
           {
                cout<<"Please enter a side lenght of the cube"<<endl;
           cin>>a;
               saoc=6*pow(a,2);
               saoc=saoc/100 ;
                cout<<"Surface Area of the Cube="<<saoc<<"m2"<<endl;
                }
          
           
           
           if ((n=2) && (u=1))
           
           {
           cout<<"Please enter the sides of prism ( width,depth,heigth)"<<endl;
           cin>>x>>y>>z;
           
           saorp=2*(x*y+x*z+y*z);
           saorp=saorp*100 ;
           cout<<"Surface Area of the Rectangular Prism="<<saorp<<"cm2"<<endl;
           }
                  
          if ((n=2) && (u=2))
           {
                cout<<"Please enter the sides of prism ( width,depth,heigth)"<<endl;
           cin>>x>>y>>z;
               saorp=2*(x*y+x*z+y*z);
           saorp=saorp/100 ;
           cout<<"Surface Area of the Rectangular Prism="<<saorp<<"m2"<<endl;
               
               }
           
           
           if((n=3) & (u=1))

           {           
           
           cout<<"Please enter the radius and height of the cylinder"<<endl;
           cin>>r>>h;
           
              saocc=2*3.1415*pow(r,2)+2*3.1415*h ;
              saocc=saorp*100 ;
           cout<<"Surface Area of the Cylinder="<<saorp<<"cm2"<<endl;   
           
           } 
                  
            if ((n=3) & (u=2))
           
           {
           
                cout<<"Please enter the radius and height of the cylinder"<<endl;
           cin>>r>>h;
             saocc=2*3.1415*pow(r,2)+2*3.1415*h ;
              saocc=saorp/100 ;
           cout<<"Surface Area of the Cylinder="<<saorp<<"m2"<<endl;    
            
            }        
               
               
               
           if ((n=4) & (u=1) )
           
           {
           cout<<"Please enter radius of the sphere"<<endl;
           cin>>k;
                saos=4*3.1415*pow(k,2) ;
              saos=saos*100 ;
           cout<<"Surface Area of the Cylinder="<<saorp<<"cm2"<<endl;   
           }   
           else 
           {
                cout<<"Please enter radius of the sphere"<<endl;
           cin>>k;
                saos=4*3.1415*pow(k,2) ;
              saos=saos/100 ;
           cout<<"Surface Area of the Cylinder="<<saorp<<"m2"<<endl;
           }
               
               
    system ("pause");
    return 0 ;
}
use == in your if statements, not =. And use && wherever you used &. And use else if instead of if on lines 36, 48, 59, 70, 83, and 97.
Last edited on
I have changed these lines according to your advice but problem is still alive :(

I have uploaded the screen picture.
You can understand the problem easily from that.


http://d1204.hizliresim.com/w/h/4gjrc.png

User chose the shape 2.
Program should have written to the screen "Please enter the sides of prism ( width,depth,heigth)" according to the algorithm but whatever you chose program calculates only for cube.
It works for me when I perform those corrections. Also, you might want to take another look at your calculations. Looks like you use the wrong variable in a lot of places. And your conversion between m^2 and cm^2 is wrong. And you have a lot of unused variables. And you should think about using nested if statements, although it's not necessary.

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
using namespace std ;

#include<iostream>
#include<cmath>
#include<fstream>

int main ()

{
    double  a,x,y,z,r,h,k,saoc,saorp,saocc,saos ;
    int n,g,o,p,j,u;
    ofstream dataout ("yenidata.txt") ;

    cout<<"Please choose the number that refers to the shape that you want to calculate of surface area "<<endl;
    cout<<"1-Cube"<<endl;
    cout<<"2-Rectangular Prism"<<endl;
    cout<<"3-Cylinder "<<endl;
    cout<<"4-Sphere"<<endl;
    cin>>n;
    cout<<"which unit you will give the values and which unit you want result"<<endl;
           cout<<"1-m2(given) -  cm2(expected)"<<endl;
           cout<<"2-cm2(given) - m2(expected)"<<endl;
           cin>>u ;
    if((n==1) && (u==1))

          {
           cout<<"Please enter a side lenght of the cube"<<endl;
           cin>>a;

                  saoc=6*pow(a,2);
                  saoc=saoc*100 ;
                  cout<<"Surface Area of the Cube="<<saoc<<"cm2"<<endl;

                  }

           else if ( (n==1) && (u==2) )

           {
                cout<<"Please enter a side lenght of the cube"<<endl;
           cin>>a;
               saoc=6*pow(a,2);
               saoc=saoc/100 ;
                cout<<"Surface Area of the Cube="<<saoc<<"m2"<<endl;
                }



           else if ((n==2) && (u==1))

           {
           cout<<"Please enter the sides of prism ( width,depth,heigth)"<<endl;
           cin>>x>>y>>z;

           saorp=2*(x*y+x*z+y*z);
           saorp=saorp*100 ;
           cout<<"Surface Area of the Rectangular Prism="<<saorp<<"cm2"<<endl;
           }

          else if ((n==2) && (u==2))
           {
                cout<<"Please enter the sides of prism ( width,depth,heigth)"<<endl;
           cin>>x>>y>>z;
               saorp=2*(x*y+x*z+y*z);
           saorp=saorp/100 ;
           cout<<"Surface Area of the Rectangular Prism="<<saorp<<"m2"<<endl;

               }


           else if((n==3) && (u==1))

           {

           cout<<"Please enter the radius and height of the cylinder"<<endl;
           cin>>r>>h;

              saocc=2*3.1415*pow(r,2)+2*3.1415*h ;
              saocc=saorp*100 ;
           cout<<"Surface Area of the Cylinder="<<saorp<<"cm2"<<endl;

           }

            else if ((n==3) && (u==2))

           {

                cout<<"Please enter the radius and height of the cylinder"<<endl;
           cin>>r>>h;
             saocc=2*3.1415*pow(r,2)+2*3.1415*h ;
              saocc=saorp/100 ;
           cout<<"Surface Area of the Cylinder="<<saorp<<"m2"<<endl;

            }



           else if ((n==4) && (u==1) )

           {
           cout<<"Please enter radius of the sphere"<<endl;
           cin>>k;
                saos=4*3.1415*pow(k,2) ;
              saos=saos*100 ;
           cout<<"Surface Area of the Cylinder="<<saorp<<"cm2"<<endl;
           }
           else
           {
                cout<<"Please enter radius of the sphere"<<endl;
           cin>>k;
                saos=4*3.1415*pow(k,2) ;
              saos=saos/100 ;
           cout<<"Surface Area of the Cylinder="<<saorp<<"m2"<<endl;
           }


    system ("pause");
    return 0 ;
}
Last edited on
system ("pause");
replace that with
std::cin.ignore();
Topic archived. No new replies allowed.