A week ago I did a three function calculator, I took the input from you guys and made it better. Overall it works fine, I didn't find any problems. But as a beginning programer I'd like some input on this calculator.
// Three function Calculator.cpp : main project file.
#include "stdafx.h"
#include<iostream>
using std::cout;
using std::cin;
long double action(long double num1, long double num2, char act) //External function to do all the calculations
{
long double total;
switch (act)
{
case '+':
total = num1 + num2;
break;
case '-':
total = num1 - num2;
break;
case '*':
total = num1*num2;
break;
case '/':
total = num1 / num2;
break;
default:
cout << "Input not recognized\n";
}
return total; //Giving the main function a number to work with.
}
int main()
{
cout << "Enter action as # to exit program\n";
cout << "Possible actions:+,-,*,/\n\n";
long double num1, num2, num3, total;
char act1, act2;
while (1) //inf loop
{
beginning: //label, to reset the program in case of division by zero
cin >> num1 >> act1 >> num2 >> act2 >> num3;
if (act1 == '#' || act2 == '#') //user wants to exit program
{
system("pause");
return 0;
}
else if (act1 == '/' && num2 == 0 || act2 == '/' && num3 == 0) //division by zero happens
{
cout << "You can't divide by zero\n";
cout << "\n\n";
goto beginning; //restarting the program from the lable, to the beginning of the 'while' loop
}
else if ((act2 == '*' || act2 == '/') && (act1 == '-' || act1 == '+')) //action2 happens before action1 because of order of operation
{
total = action(num2, num3, act2); //sending the function reciving the total
total = action(num1, total, act1); //sending the total of previos function to get overall total
}
else //action1 happens before action2 because of order of operation
{
total = action(num1, num2, act1); //sending the function reciving the total
total = action(total, num3, act2); //sending the total of previos function to get overall total
}
cout << num1 << act1 << num2 << act2 << num3 << "=" << total; //printing result of overall function
cout << "\n\n"; //new line before restarting program
}
}
Example:
Input:-6*2+4
Output:-6*2+4=-8