code problem

why does my program not properly run through the clearly defined if statements in my program? Am I missing something? I don't need an answer just some direction. Also I believe I have made it clear what I want the computer to calculate once the conditions of the while loop and if statements are met and nothing seems to be working properly. I have been stuck on this for hours and I can't figure it out. I just need some guidance. I am new to computer programming. here is my short program:

the compiler I'm using is Xcode so I don't know if that is causing any issues.

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main(void)

{
char input, Q,V,A,X;
const double Pi= 3.14159;
float r, v, a, c;


cout <<"Enter character V to find volume of a sphere, A to find the area, and X to find the Cross Section Area. Follow character input with radius. To end program input Q."<<endl;
cin >> input;

while (input!= Q)
{
cin>>r;
if (input==V)
v= ((4/3)*Pi*(r*r*r));
cout<<"The volume is:"<<v<<endl;


if(input== A)

a=4*Pi*pow(r,2);
cout<<"The surface area is:"<<a<<endl;

if(input== X)

c=Pi*pow(r,2);
cout<<"The cross sectional area is:"<<c<<endl;


cout <<"Enter character V to find volume of a sphere, A to find the area, and X to find the Cross Section Area. Follow character input with radius. To end program input Q.";
cin>>input;

}
return 0;
}
Thank you for your time.
Why aren't you using this logic?

1
2
3
if (input == V)
  {
   v = (4/3) * (Pi) * pow(r, 3.0) 
<-- this will say (4/3) * (pi) * r^3.
I think it's because your ifs aren't written correctly, then again I may be wrong. BUT I know this works for a fact some thing like this,
1
2
3
4
5
6
7
8
9
10
11
12
if (input == a)
  {
   a = 2 * 2;
  }
else if (input ==b)
  {
   b = 3 * 3;
   }
else if (input == c)
  {
   c = 4 * 4;
   }


etc etc...
Hi mohrchance,

First, please always use code tags. Edit your post, select all the code, then press the <> button on the right under the format menu.

For char variables, you need to put single quotes around the value when comparing :

while (input!= 'Q')

Same for all the instances of the input variable.

You don't need the char variables Q,V,A,X - these are just literal values.

With menu based examples like this, I like to use a switch statement inside a while loop, check this out :


http://www.cplusplus.com/forum/beginner/104553/2/#msg564228


Hope all goes well :-)

I first had it like that and then I rewrote it when I was just trying to debug that line of code and forgot to change it back to that when I made my post. Silly of me haha. I am desperate and have been trying anything and everything I can to get this simply program to compile correctly lol.
@Sweezy

Thanks for your help I'll play around with it some more!



@TheIdeasMan

I thought I posted it wrong lol I'm sorry. Thanks for your advice I'm going to try to figure it out right now! So I do not have to declare the literal variables? Xcode was making a fuss over that and the "uninitialized char variables" that I assumed cin>> would extract once the input is placed. Anyway thanks again!

-best regards to the both of you! :)
@sweezy

Your code isn't right, for the reasons given in my post.

@mohrchance

And what did you think of the example code I gave in the link?
I can't believe I have been bashing my brain in and the fix was that simple lol.
It's working perfectly now! Thanks guys!

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

using namespace std;



int main(void)

{
    char input;
    const double Pi= 3.14159;
    float r, v, a, c;
    
    
    cout <<"Enter character V to find volume of a sphere, A to find the area, and X to find the Cross Section Area. Follow character input with radius. To end program input Q."<<endl;
    cin >> input;
    
    while (input!= 'Q')
    {
        cin>>r;
        if (input=='V')
        { v= ((4/3)*Pi*pow(r,3.0));
            cout<<"The volume is:"<<v<<endl;}
   
        
        if(input== 'A')
          
        { a=4*Pi*pow(r,2.0);
            cout<<"The surface area is:"<<a<<endl;}
       
        if(input== 'X')
            
        {c=Pi*pow(r,2.0);
            cout<<"The cross sectional area is:"<<c<<endl;}
     
        
        cout <<"Enter character V to find volume of a sphere, A to find the area, and X to find the Cross Section Area. Follow character input with radius. To end program input Q.";
        cin>>input;
        
    }
    return 0;
}




@TheIdeasMan I'm now going to try to rewrite the program using that case by case switch statement. Seems more structured, and thats what I need as a beginner haha. Thank you!
v= ((4/3)*Pi*pow(r,3.0));

This doesn't do what you want either.

(4 / 3) = 1 because 4 and 3 are integers, and so integer division is performed, which discards the remainder and returns an integer value.

You need to make one of the operands a floating point type by doing something like this, (4.0 / 3).
Last edited on
@htirwin,
thanks I was wondering that myself this morning!

also im trying to figure out a method so the program will read not just one input such as:

'V5'
but if it needs to will read multiple inputs at once and deliver the results in a column so, let's say the input is:
V5
A5
X5
V7
the output will be
volume is:
Surface area is:
Cross Sectional Area is:
Volume is:

and then the prompt
'Enter character V to find volume of a sphere, A to find the area, and X to find the Cross Section Area. Follow character input with radius. To end program input Q.'

will come back up and the user can input more radii for calculation.

Thanks
is there a way to have a program generate and store additional char values and double values since the input could be anywhere from one char designated double to an infinite column of input? And then read the char tags, complete the corresponding calculations and deliver the correct numerical output with correct designations?

thank you
@ TheIdeasMan

This is the new program with the switch statement you suggested. Is this proper?
It runs fine for me! haha thanks again!
best!
-mohrchance


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

using namespace std;

const double Pi= 3.14159;

int main(void)

{
    char input;
    float r, v, a, c;
    
    
    cout <<"Enter character V to find volume of a sphere, A to find the area, and X to find the Cross Section Area. Follow character input with radius. To end program input Q.\n"<<endl;
    
do
   {
       cin >> input;
       cin>>r;
        
        switch (input)
        {
            case 'V': 
            v= ((4.0/3)*Pi*pow(r,3.0));
            cout<<"The volume of the sphere is: "<<setw(6)<<showpoint<<fixed<<setprecision(2)<<v<<endl;
                break;
                
            case 'A':
            a=4*Pi*pow(r,2.0);
            cout<<"The surface area of the sphere is: "<<setw(5)<<showpoint<<fixed<<setprecision(2)<<a<<endl;
                break;
                
            case 'X':
            c=Pi*pow(r,2.0);
            cout<<"The cross sectional area of the sphere is: "<<setw(5)<<showpoint<<fixed<<setprecision(2)<<c<<endl;
                break;
                
        }

   }while (input!= 'Q');
    
    return (0);
}
Ok, not bad, but you missed the bool variable, the quit option, and use a while loop, not a do loop, and you should have a default case in the switch to catch bad input.

With cout, you don't have to have it all in 1 statement, you can build up the output with successive calls to cout.

I had the Menu in a function of it's own, which is called from within the while loop, so the user sees it every time, not just once.

Also, prefer doubles over floats. Floats have rather low precision which is easily exceeded.

HTH
Topic archived. No new replies allowed.