please use C ++ code Analysis: create a calculator that adds, subtracts, multipl
ID: 3778659 • Letter: P
Question
please use C ++ code
Analysis: create a calculator that adds, subtracts, multiplies, divides and takes
the modulus of a number. Use functions.
Design:
output a prompt - void prompt()
get character answer from user (what function are they doing?)
a = add, s = subtract, m = multiply, d = divide, r = remainder,
q = quit
double getValue() - prompt for x, prompt for y
function to add - double add(double x, double y)
similar functions for the other operations
use a switch statement in main to choose which operation based on answer
calculate solution
output solution
Testing:
input x : 1
input y : 2
add: 3, subtract: -1, mult: 2, divide: .5, mod: 1
input x: 1
input y: 0
add: 1, sub: 1, mult: 0, divide: VALIDATE!!! cant div by zero
mod: VALIDATE!! can't div by zero
Grading criteria:
Functions
1. prompt
2. add
3. subtract
4. multiply
5. divide
6. modulus
7. getValue
Control
1. switch
2. do..while or while
3. validation
Program
1. compiles
2. logic-error free
Functions
1. prompt
2. add
3. subtract
4. multiply
5. divide
6. modulus
7. getValue
Control
1. switch
2. do..while or while
3. validation
Program
1. compiles
2. logic-error free
Explanation / Answer
#include <cstdlib.h>
#include <iostream.h>
#include<iomanip.h>
void displaymenu()
{
cout<<"==================================================="<<" ";
cout<<" MENU "<<" ";
cout<<"==================================================="<<" ";
cout<<" 1.add"<<" ";
cout<<" 2.subtract"<<" ";
cout<<" 3.multiply"<<" ";
cout<<" 4.divide"<<" ";
cout<<" 5.modulus"<<" ";
}
int substract (int a,int b)
}
int substract (int a,int b)
{
return(a-b);
int add(int a,int b)
{
return(a+b);
}
int multiply(int a,int b)
{
return(a*b);
}
float divide(int a,int b)
{
return(a/b);
}
int Modulus(int a, int b){
return(a%b);
}
int main(int argc, char *argv[])
{
//show menu
displaymenu();
int yourchoice;
int a;
int b;
char confirm;
do
{
cout<<"Enter your choice(1-5):";
cin>>yourchoice;
cout<<"Enter your two integer numbers:";
cin>>a>>b;
cout<<" ";
switch(yourchoice)
{
case 1:cout<<"Result:"<<Add(a,b);break;
case 2:cout<<"Result:"<<Substract(a,b);break;
case 3:cout<<"Result:"<<Multiply(a,b);break;
case 4:cout<<"Result:"<<Divide(a,b);break;
case 5:cout<<"Result:"<<Modulus(a,b);break;
default:cout<<"invalid";
}
cout<<" Press y or Y to continue:";
cin>>confirm;
}while(confirm=='y'||confirm=='Y');
system("PAUSE");
return EXIT_SUCCESS;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.