In the below program, how do u make it so the subtraction anddivision part wont
ID: 3611473 • Letter: I
Question
In the below program, how do u make it so the subtraction anddivision part wont be a decimal or a negative number?#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
using namespace std;
void add ();
void subtract ();
void multiply ();
void divide ();
double getnums (double&, double&,char);
void compare(double,double);
int main()
{
//double num, num1, num2;
char choice;
for (;;){
do {
cout << "***Calulator***" <<endl;
cout << "Please choose an option by entering the number, toexit the program press q." <<endl;
cout << "1 - Addition" <<endl;
cout << "2 - Subtraction" <<endl;
cout << "3 - Multiplication" << endl;
cout << "4 - Division" <<endl;
cout << "q - Quit the program"<<endl;
cin >> choice;
} while ((choice < '1' || choice > '4') && choice !='q');
if (choice == '1'){
add();}
else if (choice == '2')
subtract();
else if (choice == '3')
multiply();
else if (choice == '4')
divide();
if (choice == 'q'){
cout<<endl; break;}
//cout << num <<endl;
}
}
void add (){
double num,ans;
double num1,num2;
ans=getnums(num1,num2,'+');
num = num1 + num2;
compare(num,ans);
return ;}
void subtract (){
double num,ans;
double num1, num2;
ans=getnums(num1,num2,'-');
num = num1 - num2;
if(num < 0)
ans=getnums(num1,num2,'-');
num = num1 - num2;
compare(num,ans);
return ;}
void multiply (){
double num,ans;
double num1, num2;
ans=getnums(num1,num2,'*');
num = num1 * num2;
compare(num,ans);
return ;}
void divide (){
double num,ans;
double num1, num2;
ans=getnums(num1,num2,'/');
num = num1 /num2;
compare(num,ans);
return ;}
double getnums(double &n1, double &n2,char op)
{double ans;
n1=rand()%20+1;
n2=rand()%20+1;
cout<<"What is"<<n1<<" "<<op<<" "<<n2<<":";
cin>>ans;
return ans;
}
void compare(double a,double b)
{if(a==b)
cout<<"Great - you gotthe rigt answer!! ";
else
cout<<"I'm sorry - thecorrect answer is "<<a<<endl;
return;
}
Explanation / Answer
please rate - thanks try this and get back to me - changes in red #include #include #include #include using namespace std; void add (); void subtract (); void multiply (); void divide (); double getnums (double&, double&,char); void compare(double,double); bool check(double,double); int main() { //double num, num1, num2; char choice; for (;;){ do { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.