I'm writing a program here that should calculate the surface are, volume, cross sectional area of sphere. For each input, it should calculate what is called for, not all three answers. I should use sentinel controlled "while" loops to read the data. So here's my code, it has some issue with the SENTINEL, it doesn't compile, please help me out.
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace std;
constchar SENTINEL = Q;
int main ()
{
char A, X, V, input;
constdouble PI=3.14159;
float r, total_A, total_X, total_V;
cout<<"Enter character A to find Surface Area, character X to find the Cross Section Area,\n";
cout<<"character V to find Volume or Q to quit\n"<<endl;
cin>>input>>endl;
while (input!=SENTINEL)
{
if (input==A)
cout<<"Enter radius\n";
cin>>r>>endl;
total_A=4.0*PI*(r*r);
cout<<"The surface are is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_A<<endl;
if (input==X)
cout<<"Enter radius\n";
cin>>r>>endl;
total_X=PI*(r*r);
cout<<"The cross sectional area is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_X<<endl;
if (input==V)
cout<<"Enter radius\n";
cin>>r>>endl;
total_V=4.0/(3.0*PI*(r*r*r));
cout<<"The volume is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_V<<endl;
}
system ("pause");
return 0;
}
On the first glance, at least this line cannot be compiled: cin>>input>>endl;
because the output manipulator std::endl cannot be called by an input stream.
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace std;
constchar SENTINEL = 'Q';
int main ()
{
char A, X, V, input_char;
constdouble PI=3.14159;
float r, total_A, total_X, total_V;
cout<<"Enter character A to find Surface Area, character X to find the Cross Section Area,\n";
cout<<"character V to find Volume or Q to quit\n"<<endl;
cin>>input_char;
while (input_char!=SENTINEL)
{
if (input_char==A)
cout<<"Enter radius\n";
cin>>r;
total_A=4.0*PI*(r*r);
cout<<"The surface are is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_A<<endl;
if (input_char==X)
cout<<"Enter radius\n";
cin>>r;
total_X=PI*(r*r);
cout<<"The cross sectional area is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_X<<endl;
if (input_char==V)
cout<<"Enter radius\n";
cin>>r;
total_V=4.0/(3.0*PI*(r*r*r));
cout<<"The volume is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_V<<endl;
}
system ("pause");
return 0;
}
1: I would just be repeating what coder777 pointed out
2: What if user doesn't have caps lock on
3: Try to illuminate redundancy
4: You don't need a variable for each result
5: const (I think) should be declared outside block so whole application can see it.
This code is unconventional, so see if you can modify it without using goto and it does have a problem, but the point is to answer your question and maybe give you a hint to a better coding method and style.
I understand that my style (if I have any) and format isn't good enough. Still I'm new to it and trying to learn. On my code: It's compiling !! :) However, when I run the program and trying to type V or X (or any other letter except Q) it will only calculate for A - which is the surface area ? Can you point me out what am I doing wrong ?
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace std;
constchar SENTINEL = 'Q';
int main ()
{
char A, X, V, input_char;
constdouble PI=3.14159;
float r, total_A, total_X, total_V;
cout<<"Enter character A to find Surface Area, character X to find the Cross Section Area,\n";
cout<<"character V to find Volume or Q to quit\n"<<endl;
cin>>input_char;
while (input_char!=SENTINEL)
{
if (input_char=='A');
cout<<"Enter radius for Surface Area\n";
cin>>r;
total_A=4.0*PI*(r*r);
cout<<"The surface are is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_A<<endl;
if (input_char=='X');
cout<<"Enter radius for Cross Section Area\n";
cin>>r;
total_X=PI*(r*r);
cout<<"The cross sectional area is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_X<<endl;
if (input_char=='V');
cout<<"Enter radius for Volume\n";
cin>>r;
total_V=4.0/(3.0*PI*(r*r*r));
cout<<"The volume is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_V<<endl;
}
system ("pause");
return 0;
}
The following line does nothing: if (input_char=='A');
and neither do the other ifs.
I think you meant to write
1 2 3 4 5 6
if (input_char=='A') {
cout<<"Enter radius for Surface Area\n";
cin>>r;
total_A=4.0*PI*(r*r);
cout<<"The surface are is"<<setw(5)<<showpoint<<fixed<<setprecision(2)<<total_A<<endl;
}