c++ - Returning to the menu & exiting the program -
(c++) i'm writing program assignment , requires program prompt user return menu or quit program after each case, problem i'm not sure how implement efficiently. ideas? quitting, call last function called 'exit'
#include <iostream> //libraries using std::cout; using std::cin; using std::endl; int menu(double pi); int circlearea(double pi); int circlecircum(double pi); //function declarations demanded shapes, passing pi in order reuse in both circle calculations int rectanarea(); int triangarea(); int cubvol(); void exit(); int main() { double pi = 3.14159265359; //declaration of pi using in circles (passed) menu(pi); system("pause"); return 0; } int menu(double pi) //menu choosing shape { int choice = 0; cout << "shape calculator created by:\n\nplease select wish calculate:\n\n1 - area of circle\n\n2 - circumference of circle\n\n3 - area of rectangle\n\n4 - area of triangle\n\n5 - volume of cuboid\n\n "; cin >> choice; system("cls"); switch (choice) //switch case each shape { case 1: circlearea(pi); break; case 2: circlecircum(pi); break; case 3: rectanarea(); break; case 4: triangarea(); break; case 5: cubvol(); break; default: cout << "invalid input! please try again.\n\n"; break; } return 0; } int circlearea(double pi) //the area of circle function { double radius = 0; cout << "please enter radius of circle:\n\n"; //asks radius first cin >> radius; system("cls"); cout << "the area of circle is:\n\n" << radius*radius*pi << "cm/2\n\n"; //display calculation calculated using user input, formula pi*radius(squared) reversed return 0; } int circlecircum(double pi) //the circumference of circle function { double radius = 0.0; cout << "please enter radius of circle:\n\n"; //asks radius first cin >> radius; system("cls"); cout << "the circumference of circle is:\n\n" << pi*radius * 2 << "cm\n\n"; //calculates circumference using user input, formula 2*pi*radius reversed return 0; } int rectanarea() //function area of rectangle { double rectanheight = 0.0; double rectanwidth = 0.0; cout << "please enter height of rectangle:\n\n"; //asks input height first cin >> rectanheight; system("cls"); cout << "height = " << rectanheight << endl << endl << "please enter width of rectangle\n\n"; //shows inputted height above, asks input of width cin >> rectanwidth; system("cls"); cout << "the area of rectangle is:\n\n" << rectanheight*rectanwidth << "cm/2\n\n"; //calculates area of rectangle using input, formula height*width displays return 0; } int triangarea() //function area of triangle { double triangbase = 0.0; double triangheight = 0.0; cout << "please enter base measurement of triangle\n\n"; //asks input of base first cin >> triangbase; system("cls"); cout << "base = " << triangbase << endl << endl << "please enter height of triangle\n\n"; //displays inputted base , asks height cin >> triangheight; system("cls"); cout << "the area of triangle is:\n\n" << triangbase*triangheight / 2 << "cm/2\n\n"; //calculates area using user input , displays it, formula base*height/2 return 0; } int cubvol() //function volume of cuboid { double cublength = 0.0; double cubwidth = 0.0; double cubheight = 0.0; cout << "please enter length of cuboid\n\n"; //asks input of length first cin >> cublength; system("cls"); cout << "length = " << cublength << endl << endl << "please enter width of cuboid\n\n"; //displays inputted length , asks width cin >> cubwidth; system("cls"); cout << "length = " << cublength << endl << "width = " << cubwidth << endl << endl << "please enter height of cuboid:\n\n"; //displays inputted length , width, asks height cin >> cubheight; system("cls"); cout << "the volume of cuboid is:\n\n" << cublength*cubwidth*cubheight << "cm/3\n\n"; //displays calculation using inputted information, formula length*width*height return 0; } void exit() { cout << "**************************************" << endl; cout << "**************************************" << endl; cout << "******* t h n k y o u *******" << endl; cout << "******* f o r u s n g *******" << endl; cout << "******* t h s p r o g r m *******" << endl; cout << "**************************************" << endl; cout << "**************************************" << endl; }
in main, substitute menu(pi) with:
char choice; bool exitnow = false; do{ menu(pi); cout<<"return (r) or quit (q)?"<<endl; cin>>choice; if(choice == 'q') exitnow = true; } while (!exitnow); exit();
it should work if haven't tested it.
edit: code above doesn't check if correct input entered. should solve:
char choice = 0; bool exitnow = false; do{ menu(pi); do{ cout<<"return (r) or quit (q)?"<<endl; cin>>choice; } while( !(choice == 'r' || choice == 'q') && cout<<"invalid input!"<<endl); if(choice == 'q') exitnow = true; } while (!exitnow); exit();
Comments
Post a Comment