how to use string as command

Hello, im making a console like math calculator and i cant seem to find a way to get the string input. it cuts the string.
also, how do i read the string "add 123 456 789" and give the answer? already did a basic one when i created functions that handle multiple numbers.
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
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::string line ;
    std::getline( std::cin, line ) ; // read in a complete line

    // enter "add 123 456 789"

    // create an input stringstream which reads from the line
    std::istringstream stm(line) ;
    std::cout << "calculate: " << line << '\n' ;

    // extract the command
    std::string oper ;
    stm >> oper ;

    if( oper == "add" )
    {
        int result = 0 ;
        int number ;
        while( stm >> number ) // for each number read from line
            result += number ;
        std::cout << "result: "  << result << '\n' ;
    }

    // else if( oper == "subtract" ) etc.

}
thanks. helped a lot. would you please explain this so i can understamd! thanks
Topic archived. No new replies allowed.