Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

see photo below!!! . Write a C++ program that asks users for two numbers and a c

ID: 3540311 • Letter: S

Question

see photo below!!!

.

Write a C++ program that asks users for two numbers and a choice of the following operations: addition, subtraction, multiplication, division or exit the program. The program then displays the result of chosen operations on the two numbers entered or terminates. Use one function to get two numbers, separate functions for each arithmetic operations (+,-,*, /), and one function to display the output. The program should continue running until the user chooses to exit. Don't forget to do data validation on choice of operations and avoid 'divide by 0' error in the Division function.

Explanation / Answer

please rate - thanks



#include <iostream>
using namespace std;
int multiply(int,int);
double divide(int,int);
int add(int,int);
int subtract(int,int);
void display(int,int,double,char);
void getNumbers(int&,int&);
int getOperation();
int main()
{int choice,n1,n2;
double result;
do
{choice=getOperation();
if(choice<5)
   {getNumbers(n1,n2);
    if(choice==1)
       {result=add(n1,n2);
        display(n1,n2,result,'+');
        }
    else if(choice==2)
       {result=subtract(n1,n2);
       display(n1,n2,result,'-');
       }
   else if(choice==3)
       {result=multiply(n1,n2);
       display(n1,n2,result,'*');
       }    
   else if(choice==4)
       {if(n2==0)
          cout<<"Cannot divide by 0 ";
       else
         {result=divide(n1,n2);
          display(n1,n2,result,'-');
          }
     }
}
else if(choice!=5)
      cout<<"Invalid operation ";
}while(choice!=5);    
return 0;
}
int multiply(int i,int j)
{return i*j;
}
double divide(int i,int j)
{return (double)i/j;
}
int add(int i,int j)
{return i+j;
}
int subtract(int i,int j)
{return i-j;
}
void display(int n1,int n2,double result,char sign)
{cout<<n1<<" "<<sign<<" "<<n2<<" = "<<result<<endl<<endl;
}
void getNumbers(int& n1,int& n2)
{cout<<"Enter 1st number: ";
cin>>n1;
cout<<"enter 2nd number: ";
cin>>n2;
}
int getOperation()
{int oper;
cout<<"What operation would you like to do? ";
cout<<" 1 -addition ";
cout<<" 2 -subtraction ";
cout<<" 3 -multiplication ";
cout<<" 4 -division ";
cout<<" 5 -exit the program ";
cin>>oper;
return oper;
}