ok i posted something similar earlier but i've refined it and it works almost how i want, but not quite there. i know the code is messy but that's not the issue right now. i'm trying to set a menu so that a user can choose 4 shapes to calculate the surface area of. i have to use functions for the menu, the surface area, and the "getvalue" thing. that was giving me issues before but i've gotten that working. so the problem i'm having now is that when i run the program, the menu will display, and will display again after each shape, but the input for the menu does nothing. so if sphere is 1, prism is 2, cone is 3, pyramid 4, entering any of those does nothing. and entering 5 doesn't exit my while loop. it just runs through the shapes 1, 2, 3, 4 and then back to the top. what am i doing wrong? i just need to get the menu function, and the number the user inputs in there to work with the other functions in the program.
Code:#include <iostream> #include <iomanip> #include <string> #include <math.h> using namespace std; const double PI = 3.14159; int value,choice, radius; float surface_area_sphere, surface_area_prism, surface_area_cone, surface_area_pyramid; int menu() { cout<<"\nSurface Area Calculator" <<"\n1) Sphere" <<"\n2) Cone" <<"\n3) Rectangular Prism" <<"\n4) Pyramid" <<"\n\n5) Quit" <<"\n\nEnter Your Choice: "; cin>>choice; while (choice<1 || choice>5) { cout<<"\nInvalid choice, please try again"; cin>>choice; } return(0); } float calcSphere (int radius) { surface_area_sphere=(4*PI*radius*radius); cout<<"The surface area of the sphere is"<<fixed<<setprecision(4)<<surface_area_sphere; return (surface_area_sphere); } int getvalue (string prompt, int lowerBound, int upperBound) { cout<<prompt<<lowerBound<<" - "<<upperBound; cin>>value; while (value<lowerBound || value>upperBound) { cout<<"invalid number, please try again"; cin>>value; return (value); } } float calcPrism (int length, int width, int height) { surface_area_prism = ( 2 * length * width ) + ( 2 * width * height ) + ( 2 * length * height ); cout<<"\nThe surface area of the prism is:"<<fixed<<setprecision(4)<<surface_area_prism; return(surface_area_prism); } float calcCone(int radius, int length) { surface_area_cone = ( PI * radius * length ) + ( PI * radius*radius ); cout<<"\nThe surface area of the cone is:"<<fixed<<setprecision(4)<<surface_area_cone; return (surface_area_cone); } float calcPyramid (int length, int height) { surface_area_pyramid = (2*length*height) + (length*length); cout<<"\nThe surface area of the pyramid is: "<<fixed<<setprecision(4)<<surface_area_pyramid; return (surface_area_pyramid); } int main() { while (choice !=5) { menu(); if (choice=1) { value=getvalue("\nenter the radius of the sphere", 1, 10); calcSphere (value) ; } menu(); if (choice=2) { value=getvalue("\nenter the length of the prism", 1, 20); value=getvalue("\nenter the width of the prism", 1, 20); value=getvalue("\nenter the height of the prism", 1, 20); calcPrism (value, value, value); } menu(); if (choice=3) { value=getvalue("\nenter the radius of the cone", 1,7); value=getvalue("\nenter the length of the cone", 1, 12); calcCone (value, value); } menu(); if (choice=4) { value=getvalue("\nenter the length of the pyramid", 1, 10); value=getvalue("\nenter the height of the pyramid", 1, 15); calcPyramid (value, value); } } return 0; }



LinkBack URL
About LinkBacks


