Created
June 2, 2018 00:21
-
-
Save MohamedTaha98/f5e812c61313302e926a902c7d2304d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
void read2Numbers(double &dNum1, double &dNum2); | |
int menu(); | |
bool sum(double dNum1, double dNum2, double &dResult); | |
bool sub(double dNum1, double dNum2, double &dResult); | |
bool multi(double dNum1, double dNum2, double &dResult); | |
bool divide(double dNum1, double dNum2, double &dResult); | |
void runCal(); | |
int main(void) | |
{ | |
cout << "\t\t\t\tWelcome to The Calculator :D \n\n"; | |
runCal(); | |
return 0; | |
} | |
void read2Numbers(double &dNum1, double &dNum2) | |
{ | |
dNum1 = dNum2 = 0; | |
cout << "Please Enter 2 Numbers: \n"; | |
cin >> dNum1 >> dNum2; | |
if (cin.fail()) | |
{ | |
cout << "Retry: \n"; | |
cin.clear(); | |
cin.ignore(10000, '\n'); | |
read2Numbers(dNum1, dNum2); | |
} | |
return; | |
} | |
int menu() | |
{ | |
int iChoice = 0; | |
cout << "Please Enter Your Choice: \n1: Sum, 2: Subtraction, 3: Multiply, 4:Divide, 5:Input 2 Numbers Again: \n"; | |
cin >> iChoice; | |
if (cin.fail()) | |
{ | |
cout << "Retry: \n"; | |
cin.clear(); | |
cin.ignore(10000, '\n'); | |
return menu(); | |
} | |
return iChoice; | |
} | |
bool sum(double dNum1, double dNum2, double &dResult) | |
{ | |
dResult = dNum1 + dNum2; | |
return true; | |
} | |
bool sub(double dNum1, double dNum2, double &dResult) | |
{ | |
dResult = dNum1 - dNum2; | |
return true; | |
} | |
bool multi(double dNum1, double dNum2, double &dResult) | |
{ | |
dResult = dNum1 * dNum2; | |
return true; | |
} | |
bool divide(double dNum1, double dNum2, double &dResult) | |
{ | |
if (dNum2 == 0) | |
{ | |
cout << "Cannot Divide by Zero!\nRetry: "; | |
return false; | |
} | |
dResult = dNum1 / dNum2; | |
return true; | |
} | |
void runCal() | |
{ | |
double dNum1 = 0, dNum2 = 0, dResult = 0; | |
int iChoice = 0; | |
bool bFinished = false; | |
while (!bFinished) | |
{ | |
read2Numbers(dNum1, dNum2); | |
iChoice = menu(); | |
if (iChoice == 5) | |
continue; | |
else if (iChoice == 1) | |
bFinished = sum(dNum1, dNum2, dResult); | |
else if (iChoice == 2) | |
bFinished = sub(dNum1, dNum2, dResult); | |
else if (iChoice == 3) | |
bFinished = multi(dNum1, dNum2, dResult); | |
else if (iChoice == 4) | |
bFinished = divide(dNum1, dNum2, dResult); | |
} | |
cout << "Result = " << dResult << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment