Please Help! how to make a program in c++ using switch statement. Sample Run: 1 - Addition 2 - Subs 3 - Multiplication 4- Division 5 - Modulo Division 6- Average 7- Quit
cout<<"MENU OF OPERATION"; cout<<endl; cout<<"1 - addition\n"; cout<<"2 - subtraction\n"; cout<<"3 - multiplication\n"; cout<<"4 - division\n"; cout<<"5 - Modulo Division\n";\ cout<<"6 - Average\n"; cout<<"7 - Quit\n"; cout<<endl; cout<<endl; cout<<"Enter your choice: "; cin>> choice; switch (choice) { case '1': cout<<"***You Have Chosen Addition!***\n"; cout<<"Please Enter the first value:"; cin>>a; cout<<"Please Enter the second value:"; cin>>b; cout<<"Please Enter the third value:"; cin>>c; cout<<endl; Add = a + b + c; cout<<"The sum of "<<a<<" and "<<b<< " and" <<c<< " is = "<<Add<<endl;break; cout<<endl; case '2': cout<<"***You Have Chosen Subtruction!***\n"; cout<<"Please Enter the first value:"; cin>>a; cout<<"Please Enter the second value:"; cin>>b; cout<<"Please Enter the third value:"; cin>>c; cout<<endl; Sub = a - b - c; cout<<"The Difference of "<<a<<" and "<<b<<" and " <<c<< " is = "<<Sub<<endl;break; cout<<endl; case '3': cout<<"***You Have Chosen Multiplication!***\n"; cout<<"Please Enter the first value:"; cin>>a; cout<<"Please Enter the second value:"; cin>>b; cout<<"Please Enter the third value:"; cin>>c; cout<<endl; Multi = a * b *c; cout<<"The product of "<<a<<" and "<<b<< "and" <<c<< " is = "<<Multi<<endl;break; cout<<endl; case '4': cout<<"***You Have Chosen Division!***\n"; cout<<"Please Enter the first value:"; cin>>a; cout<<"Please Enter the second value:"; cin>>b; cout<<"Please Enter the third value:"; cin>>c; cout<<endl; Divi = a / b / c; cout<<"The qoutient of "<<a<<" and "<<b<< " and " <<c<< " is = "<<Divi<<endl;break; cout<<endl; case '5': cout<<"***You Have Chosen Modulo Division!***\n"; cout<<"Please Enter the first value:"; cin>>a; cout<<"Please Enter the second value:"; cin>>b; cout<<"Please Enter the third value:"; cin>>c; cout<<endl; Mod = a % b % c; cout<<"The Remainder of " <<a<< " and " <<b<< " and " <<c<< " is = "<<Mod<<endl;break; cout<<endl; case '6': cout<<"***You Have Chosen Average!***\n"; cout<<"Please Enter the first value:"; cin>>a; cout<<"Please Enter the second value:"; cin>>b; cout<<"Please Enter the third value:"; cin>>c; Ave = a + b + c/3; cout<<"The Average of " <<a<< " and " <<b<< " and " <<c<< " is = "<<Ave<<endl;break; cout<<endl; case '7': cout<<"***You Have Chosen Quit!***\n"; cout<<"*****GOODBYE!!! PLEASE COME AGAIN!*****"<<endl;break; default: cout<<"INVALID CHOICE!\n"; } system("pause"); return 0; --- this is my sample program. but when i started diving 0 it prompted "Unhandled exception at 0x00041ee7 in Exercise1(switch).exe: 0xC0000094: Integer division by zero."/
dividing*
You can't divide anything by zero; you have to check operands before performing some operations; by the way, in the operation n° 6 (average) you wrote: Ave = a + b + c/3; That's a bad formula :-) ; you need brackets: Ave = (a + b + c)/3;
How about adding if & else statement? is it possible?
within each "case" you can write any instruction set
Join our real-time social learning platform and learn together with your friends!