In: Computer Science
Instructions for C++ project
This Assignment will Focus on Software Engineering: Using Functions in a Menu Driven Program
Hint: Chapter 6 Program 6-10 is a great example of this type of programs.
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user.
1 - Add
2 - Subtract
3 - Multiply
4 - Divide
5 - Raise X to the power Y
6 - Finds if a number is even or odd
0 - Quit
Your calculator must:
1. Display a menu with all the options
2- Ask the user to select an option from the menu
3. Based on the user selection, your program must then ask for the
operand/s and call the pertaining function.
4. Your program will accept int and the result can be displayed in double (utilize type casting).
5.Once your program is done with the user's option, your program must ask if the user would like to try another option utilizing loops)
Input Validation: Your program should only accepts numbers.
Answer:
Here is the c++ code as per your requriement
Raw code:
//including headers
#include <iostream>
#include <math.h>
//functions for the options
using namespace std;
//addition
int sum(int a,int b){
return a+b;
}
//subtraction
int sub(int a,int b){
return a-b;
}
//mutlitply
int mul(int a,int b){
return a*b;
}
//divioson
double divison(int a,int b){
return (double)a/b;
}
//exponention
double power(int a,int b){
return (double)pow(a,b);
}
//odd or even
string odd_even(int a){
if(a%2==0){
return "Even";
}
return "Odd";
}
int main() {
//main print menu
cout<<"1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide\n5 - Raise X to the power Y\n6 - Finds if a number is even or odd\n0 - Quit"<<endl;
char cho= 'y';
//do while loop
do{
//get input
int choice;
//if user entered 0 break
cin>>choice;
if (choice==0){
break;
}
//else
else{
// call the functions based on choice
if(choice==1){
int a;int b;
cout<<"Enter values with spaces: ";
cin>>a>>b;
cout<<"Sum: "<<sum(a,b)<<endl;
}
if(choice==2){
int a;int b;
cout<<"Enter values with spaces: ";
cin>>a>>b;
cout<<"Sub: "<<sub(a,b)<<endl;
}
if(choice==3){
int a;int b;
cout<<"Enter values with spaces: ";
cin>>a>>b;
cout<<"Multiplication: "<<mul(a,b)<<endl;
}
if(choice==4){
int a;int b;
cout<<"Enter values with spaces: ";
cin>>a>>b;
cout<<"Divison: "<<divison(a,b)<<endl;
}
if(choice==5){
int a;int b;
cout<<"Enter values with spaces: ";
cin>>a>>b;
cout<<a<<" raises to the power "<<b<<power(a,b)<<endl;
}
if(choice==6){
int a;
cin>>a;
cout<<odd_even(a)<<endl;
}
cout<<"would you like to try other option?(y/n) "<<endl;
cin>>cho;
}
//if user enterd n exit the progrm
}while(cho!='n');
}
Editor:
output:
Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.
"Please refer to the screenshot of the code to understand the indentation of the code".
Thank you! Do upvote.