Hello, I am having trouble with a program that is meant to calculate the volume of a sphere, cylinder, and so on. I suspect I have a small error in my code causing it to skip over the volume statements for the sphere. If anyone could give it a quick look, I would greatly appreciate some help, Thanks.
#include <iostream>
#include <math.h>
#define PI 3.1415926535897932384626
usingnamespace std;
int main()
{
start:
char restart;
char shape;
double radius;
double height;
int sides;
double halfsidelength;
double sidelength;
double apothem;
double Base;
double perimeter;
double slantheight;
char sora;
char slantheightq1;
cout << "Geometric figure surface area and volume calculator\n";
cout << "What kind of shape are you using?\n";
cout << "Sphere(s), Cylinder(y), Prism(p), Pyramid(d)";
cin >> shape;
switch(shape){
case's':
cout << "Are you calculating the surface area(s) or the volume(v)?";
cin >> sora;
if(sora=='s'){//This is for the surface area
cout << "Enter the radius: ";
cin >> radius;
cout << "Surface Area = "<<(4*PI*(radius*radius))<<endl;
cout << "Would you like to restart the program?(Y/N)";}
cin >> restart;
if(restart=='y'){
goto start;}
elseif(restart=='n'){
cout << "Thank you for using the geometric figure calculator";}
elseif(sora=='v'){//This is for the volume
cout << "Enter the radius: ";
cin >> radius;
cout << "Volume = "<<((4*PI*(radius*radius*radius))/3);
cout << "Would you like to restart the program?(Y/N)";}
cin >> restart;
if(restart=='y'){
goto start;}
elseif(restart=='n'){
cout << "Thank you for using the geometric figure calculator";}
break;
case'y':
cout << "Are you calculating the surface area(s) or the volume(v)?";
cin >> sora;
if(sora=='s'){
cout << "Enter the radius: ";
cin >> radius;
cout << "Enter the height: ";
cin >> height;
cout << "Surface Area = "<<((2*(PI*(radius*radius))+(2*(PI*radius)*height)));
cout << "\nWould you like to restart the program?(Y/N)";}
cin >> restart;
if(restart=='y'){
goto start;}
elseif(restart=='n'){
cout << "Thank you for using the geometric figure calculator";}
elseif(sora=='v'){
cout << "Enter the radius: ";
cin >> radius;
cout << "Enter the height: ";
cin >> height;
cout << "Volume = "<<(PI*(radius*radius)*height);
cout << "Would you like to restart the program?(Y/N)";}
cin >> restart;
if(restart=='y'){
goto start;}
elseif(restart=='n'){
cout << "Thank you for using the geometric figure calculator";}
break;
default:
cout << "I apologize, we do not currently support this figure\n";
cout << "Would you like to restart the program?(Y/N)";
cin >> restart;
if(restart=='y'){
goto start;}
elseif(restart=='n'){
cout << "Thank you for using the geometric figure calculator\n";}
break;
}
return(0);
}
The logic of your program seems to be sound, but see this example. It's a lot cleaner code to look at, therefore easier to debug and the interface for the user much simpler.
#include <iostream>
#include <math.h>
constdouble PI = 3.1415926535897932384626;
usingnamespace std;
int main() {
cout << "Geometric figure surface area and volume calculator\n\n";
cout << "\t[S] Sphere\n\t[C] Cylinder\n\t[P] Prisim\n\t[Y] Pyramid\n\t[Q] Quit\n";
do {
char Selection, SubSelection;
double Result, Radius;
cout << "\nPlease choose S C P Y or Q: ";
cin >> Selection;
Selection &= 0x5F; // Convert to uppercase
if ( Selection == 'Q' )
break; // Terminate infinite loop
switch ( Selection ) {
case'S':
cout << "\nSpheres radius? ";
cin >> Radius;
Result = 4 * PI * (Radius * Radius);
cout << "\nSurface Area = " << Result << endl;
Result = (4 * PI * (Radius * Radius * Radius)) / 3;
cout << " Volume = " << Result << endl;
break;
case'Y':
break;
case'P':
case'C':
cout << "\tSorry " << Selection << " has not been implemented yet\n";
break;
default:
cout << "\t\"" << Selection << "\" is not a valid selection\n";
}
} while (1);
cout << "\nBye Bye\n";
return(0);
}
Try to reuse variables as much as possible as most declarations in lines 10 - 22 in your example can be accommodated by Result. You shouldn't have too much problem implementing for the other selections.