Edit: I made a new calculator after taking your input New Calculator
I made this three-function calculator. I have been learning C for a couple of months, and I learned C++ through the internet. This is my first "real" program in C++, it started really messy but I did a lot of refining and debuging and deleting unnecessary code and this is the final result. If you see any way to improve it or something I did wrong please tell me.
// Three numbers Calculator
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
cout << "Enter action as # to exit program" << endl;
cout << "Possible actions:+,-,*,/\n" << endl;
while (1) //loop
{
long double num1, num2, num3, total;
char action1, action2;
cin >> num1 >> action1 >> num2 >> action2 >> num3;
if (action1 == '/' && num2 == 0 || action2 == '/' && num3 == 0)
cout << "You can't divide by zero" << endl;
else if ((action2 == '*' || action2 == '/') && (action1 == '-' || action1 == '+')) //action2 will be preformed before action1 (Order of operation)
{
switch (action2) //I didn't include the options for '+' or '-' because the if statement requires action2 to be '*' or '/'.
{
case('/'):
total = num2 / num3;
break;
case('*'):
total = num2*num3;
break;
default:
cout << "Input not recognized";
break;
}
switch (action1) //I didn't include the options for '*' or '/' because the if statement requires action1 to be '+' or '-'.
{
case('+'):
cout << num1 << "+" << num2 << action2 << num3 << "=" << total + num1;
break;
case('-'):
cout << num1 << "-" << num2 << action2 << num3 << "=" << num1 - total;
break;
default:
cout << "Input not recognized";
break;
}
}
else //action1 will be performed before action2 (Order of operation)
{
switch (action1)
{
case('+'):
total = num1 + num2;
break;
case('-'):
total = num1 - num2;
break;
case('/'):
total = num1 / num2;
break;
case('*'):
total = num1*num2;
break;
case('#'):
system("PAUSE");
return 0;
break;
default:
cout << "Input not recognized";
break;
}
switch (action2)
{
case('+'):
cout << num1 << action1 << num2 << "+" << num3 << "=" << total + num3;
break;
case('-'):
cout << num1 << action1 << num2 << "-" << num3 << "=" << total - num3;
break;
case('/'):
cout << num1 << action1 << num2 << "/" << num3 << "=" << total / num3;
break;
case('*'):
cout << num1 << action1 << num2 << "*" << num3 << "=" << total*num3;
break;
case('#'):
system("PAUSE");
return 0;
break;
default:
cout << "Input not recognized";
break;
}
}
cout << "\n\n";
}
}
Update:
Input: 7+8/2
Output: 7+8/2=11
using namespace System;
is needed in your code. This is more like C#, please edit. \$\endgroup\$using namespace std;
. \$\endgroup\$