PLEASE CODE IN C++ LANGUAGE. Write a simple calculator program that repeatedly a
ID: 3880499 • Letter: P
Question
PLEASE CODE IN C++ LANGUAGE.
Write a simple calculator program that repeatedly allows the user to select +,- from a menu of operations on real values and then enter the two operands which then computes the result of applying the selected operation to those operands. However, the only named variables you may use are pointer variables; all others must be anonymous variables. Pay close attention to the following aspects: error checking loop function pointer variables code efficiency(smart code), readability, documentation.Explanation / Answer
#include<iostream>
using namespace std;
void read_numbers(int *p,int *q)//to read input
{
cout<<"Enter operand1:";
cin>>*p;
cout<<"Enter operand2:";
cin>>*q;
}
void add()
{
int *p=new int(),*q=new int();//creating integers
read_numbers(p,q);
cout<<"Output: "<<(*p)+(*q)<<endl;
delete(p);//clearing space
delete(q);
}
void sub()
{
int *p=new int(),*q=new int();//creating integers
read_numbers(p,q);
cout<<"Output: "<<(*p)-(*q)<<endl;
delete(p);//clearing space
delete(q);
}
void mul()
{
int *p=new int(),*q=new int();//creating integers
read_numbers(p,q);
cout<<"Output: "<<(*p)*(*q)<<endl;
delete(p);//clearing space
delete(q);
}
void div()
{
int *p=new int(),*q=new int();//creating integers
read_numbers(p,q);
cout<<"Output: "<<(*p)/(*q)<<endl;
delete(p);//clearing space
delete(q);
}
int main()
{
int *p = new int();
while(1)//loop running repeatedly
{
//displaying menu
cout<<"1: + 2: - 3: * 4: / Enter option:";
cin>>*p;
if(*p==1)
{
add();//calling add
}
else if(*p==2)
{
sub();//calling sub
}
else if(*p==3)
{
mul();//calling mul
}
else if(*p==4)
{
div();//calling div
}
else
{
cout<<"Wrong option! ";
}
}
return 0;
}
output:
1: +
2: -
3: *
4: /
Enter option:1
Enter operand1:2
Enter operand2:3
Output: 5
1: +
2: -
3: *
4: /
Enter option:2
Enter operand1:5
Enter operand2:2
Output: 3
1: +
2: -
3: *
4: /
Enter option:3
Enter operand1:2
Enter operand2:3
Output: 6
1: +
2: -
3: *
4: /
Enter option:4
Enter operand1:6
Enter operand2:2
Output: 3
1: +
2: -
3: *
4: /
Enter option:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.