Split a string?

Hi. I am making a calculator, and I manage to add two numbers with two lines and two different inputs. I am now trying to make it avaliable to input two numbers on the same line. Like one input for two numbers who will add. Is there a way to do this? And while I am on it, how can I make it avaliable to input an operator like "+", "-", "/" and "*"?

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int result, num1, num2;
  
int main()
{
cout << "Plase enter the numbers you want to add: ";
cin >> num1;
cin >> num2;
result = num1+num2;
cout << "The result is: " << result << endl;
}


Here I have two "cin's" and I want to make it one, if you get it. Is there a way to do that? :)
your code already works for multiple numbers on the same line.
http://cpp.sh/5rvz

yoou can say
cin >> num1 >> num2;
its exactly the same thing as you wrote.

just enter the two numbers with a space between them, it counts as whitespace, just the same as carriage return.

or add this.
1
2
string op;
cin >> num1 >> op >> num2;


you can then of course process the op... this code wont compile of course, op is a string and i'm using it as a char to demonstrate the point.
1
2
3
4
5
6
7
8
9
10
11
12
 
switch (op)
{
case '+':
    ans = num1 + num2;
    break;
case '-':
    ans = num1 - num2;
    break;
case '*':
    ans = num1 * num2;
    break;

Thank you so much! :) It worked.
Topic archived. No new replies allowed.