double a; // to hold the user's first integer
double b; // to hold the user's second integer
char operation; // to hold the user's operation: I changed this to a single char
// Read the equation into line
cout << "Please enter an equation: ";
string line;
getline(cin, line);
cout << endl;
// extract the args and operator
istringstream iss(line);
iss >> a >> operation >> b;
// copied from your code
if (operation == '+')
cout << a + b;
elseif (operation == '-')
cout << a - b;
elseif (operation == '*')
cout << a * b;
elseif (operation == '/')
cout << a / b;
else
cout << "ERROR: Operation not supported: " << operation;
#include <iostream>
#include<conio.h>
using std::cin;
using std::cout;
using std::endl;
int main() {
double a; // to hold the user's first integer
double b; // to hold the user's second integer
char operation; // to hold the user's operation
cout<<"Please enter an expression (a number, operator, other number)\n\n";
a=getche()-'0';
operation=getche();
b=getche()-'0';
cout << endl;
cout << a << " " << operation << " " << b << " = ";
if (operation == '+')
cout << a + b;
elseif (operation == '-')
cout << a - b;
elseif (operation == '*')
cout << a * b;
elseif (operation == '/')
cout << double(a) / double(b);
else
cout << "ERROR: Operation not supported";
cout << endl;
return 0; // success
}
Don't do that.
It assumes your numbers are 1 digit integers
It assumes that you're using an old Borland compiler, conio.h and getche are specific for particular environments and not standard.
you are absolutely correct.
that's why I've used "maybe" in the first line of my post.
It assumes that you're using an old Borland compiler
It doesn't, it works on most of the compilers. Yes, they are non-standard, but still works and maybe ,we shouldn't hesitate using them unless we are writing cross-platform programs