How to enter all at once?

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
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>.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    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::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 

#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;
    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.