### How to enter all at once?

How do I enter the equation all at once instead of prompting for each piece separately?

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include 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 string operation; // to hold the user's operation cout << "Please enter an integer: "; cin >> a; cout << "Please enter an operation (+, - , *, /): "; cin >> operation; cout << "Please enter another integer: "; cin >> b; cout << endl; cout << a << " " << operation << " " << b << " = "; if (operation == '+') cout << a + b; else if (operation == '-') cout << a - b; else if (operation == '*') cout << a * b; else if (operation == '/') cout << double(a) / double(b); else cout << "ERROR: Operation not supported"; cout << endl; return 0; // success }``````
You can enter it as a string, then parse it.

For example, if we don't have spaces in the input string, the string is going to be <nn><op><nn>.
 ``12345678910111213141516171819202122232425`` `````` 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; else if (operation == '-') cout << a - b; else if (operation == '*') cout << a * b; else if (operation == '/') cout << a / b; else cout << "ERROR: Operation not supported: " << operation;``````
Last edited on
or the other alternative maybe using `getche()` from conio.h::

 ``12345678910111213141516171819202122232425262728293031323334353637`` `````` #include #include 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; else if (operation == '-') cout << a - b; else if (operation == '*') cout << a * b; else if (operation == '/') cout << double(a) / double(b); else cout << "ERROR: Operation not supported"; cout << endl; return 0; // success }``````
Last edited on
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.
@kbw

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
Last edited on
Topic archived. No new replies allowed.