Lab 07 Void functions Objective To practice using void functions. To begin using
ID: 3804013 • Letter: L
Question
Lab 07 Void functions Objective To practice using void functions. To begin using pass-by-reference parameters. Tasks In this week's lab, we have a few rules to follow: All functions must be void functions No cin or cout statements in int 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 andband 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. Don't 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 5Explanation / Answer
#include <iostream>
#include<conio.h>
using namespace std;
void menu(int *);
void getNums(int *,int *);
void add(int,int);
void sub(int,int);
void mul(int,int);
void divi(int,int);
int main()
{
int usrChoice=0,a,b;
while(usrChoice!=5)
{
//pass by reference for usechoice
menu(&usrChoice);
switch(usrChoice)
{
case 1:
//pass by refernce for user input
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);
divi(a,b);
break;
case 5:
break;
}
}
getch();
return 0;
}
//void function to display menu
void menu(int *usrChoice)
{
cout << "==Main Menu=="<<endl;
cout << "1 - add 'a' to 'b'"<<endl;
cout << "2 - subtract 'b' from 'a'"<<endl;
cout << "3 - multiply 'a' by 'b'"<<endl;
cout << "4 - divide 'a' by 'b'"<<endl;
cout << "5- exit"<<endl;
cin >> *usrChoice;
}
//void function to read input from user
void getNums(int *a,int *b)
{
cout << "Enter number a:";
cin >> *a;
cout << "Enter number b:";
cin>>*b;
}
//void function to add two numbers
void add(int a,int b)
{
cout <<"a + b " << a+b <<endl;
}
//void function to subtract
void sub(int a,int b)
{
cout <<"a - b " << a-b <<endl;
}
//void function to multiply two numbers
void mul(int a,int b)
{
cout <<"a * b " << a*b <<endl;
}
//void function to divide
void divi(int a,int b)
{
cout <<"a / b " << a/b <<endl;
}
Output :
==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:2
Enter number b:3
a + b 5
==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:4
Enter number b:2
a - b 2
==Main Menu==
1 - add 'a' to 'b'
2 - subtract 'b' from 'a'
3 - multiply 'a' by 'b'
4 - divide 'a' by 'b'
5- exit
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.