Skip to content

Instantly share code, notes, and snippets.

@MohamedTaha98
Created June 2, 2018 00:21
Show Gist options
  • Save MohamedTaha98/f5e812c61313302e926a902c7d2304d5 to your computer and use it in GitHub Desktop.
Save MohamedTaha98/f5e812c61313302e926a902c7d2304d5 to your computer and use it in GitHub Desktop.
#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