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.
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;
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.
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!
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace std;
int main(void)
{
char input;
constdouble 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!
@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.
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?
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
constdouble 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.