Question
Can't find what is wrong with my program! It won't run. I think that it has something to do with division?? a few rules to
Can't find what is wrong with my program! It won't run. I think that it has something to do with division??
a few rules to follow:
All functions must be void functions.
Nocinorcoutstatementsinint main.
No global variables.
As a consequence, all your functions that need to return a value will have to handle that via pass-by-reference parameters.
Implement the following functions:
void menu(int& usrChoice); Outputs the main menu and then stores the value the user inputted in usrChoice.
void getNums(int& a, int& b); Asks the user to enter values for a and b and stores the responses in the corresponding variables.
void add(int a, int b); Adds the values of a and b and outputs the result.
void sub(int a, int b); Subtracts b from a and outputs the result.
void mul(int a, int b); Multiplies a by b and outputs the result.
void div(int a, int b); Divides a by b and outputs the result.
Dont alter the prototypes. Get your program to match the following output and also to be able to handle different values of a and b according to the menu actions the user chooses.
Sample Output (user input is italicized)
==Main Menu== 1 - add 'a' to 'b' 2 - subtract 'b' from 'a' 3 - multiply 'a' by 'b' 4 - divide 'a' by 'b' 5 - exit >1 Enter number a: 4 Enter number b: 5 a+b=9
==Main Menu== 1 - add 'a' to 'b' 2 - subtract 'b' from 'a' 3 - multiply 'a' by 'b' 4 - divide 'a' by 'b' 5 - exit >2 Enter number a: 10 Enter number b: 3 a-b=7
==Main Menu== 1 - add 'a' to 'b' 2 - subtract 'b' from 'a' 3 - multiply 'a' by 'b' 4 - divide 'a' by 'b' 5 - exit >3 Enter number a: 4 Enter number b: 5 a * b = 20
==Main Menu== 1 - add 'a' to 'b' 2 - subtract 'b' from 'a' 3 - multiply 'a' by 'b' 4 - divide 'a' by 'b' 5 - exit >4 Enter number a: 101 Enter number b: 20 a / b = 5.05
This is my code: #includeusing namespace std; // Displaying menu and reading users choice void menu(int& usrChoice) { cout<<" ==Main Menu=="< "1 - add 'a' to 'b'"< "2 - subtract 'b' from 'a'"< "3 - multiply 'a' by 'b'"< "4 - divide 'a' by 'b'"< "5 - exit"< >usrChoice; } // Reading numbers void getNums(int& a, int& b) { cout<<"Enter number a: "; cin>>a; cout<<"Enter number b: "; cin>>b; } // calculating addition void add(int a, int b) { cout<<"a+b="<// calculating substraction void sub(int a, int b) { cout<<"a-b="< //calculating multiplication void mul(int a, int b) { cout<<"a*b="<//calculating division void div(int a, int b) { cout<<"a/b="<int main() { // continues the loop until the user want to exit from it (means enter choice 5) while(true) { int usrChoice; //reading users choice menu(usrChoice); switch(usrChoice) { int a,b; case 1: getNums(a,b); add(a,b); break; case 2: getNums(a,b); sub(a,b); break; case 3: getNums(a,b); mul(a,b); break; case 4: getNums(a,b); div(a,b); break; case 5: exit(0); } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started