vaibhava96 vaibhava96 - 4 months ago 42
C++ Question

splitting string into integer and string C++

As a part of my project, I am expected to receive input in the following format.


where A is a letter (any capital), and XXX is any integer (note that this number does not have to be limited to 3 digits). The letter is associated with some functionality (e.g. A means Add XXX to a list of numbers). I want to make a function that can read each term in the input and recognise what actions need to be taken (e.g. if I put A123 A983 A828 i know that i want to store the numbers 123 983 and 828 on a list). I hope that this was not confusing. If you are wondering why I am doing this, my project is on linked lists and requires input from the user to add nodes to the linked list.


This should work, but it does check for errors. It assume data is coming in proper format and order.

using namespace std;

int main()
    string input( "A123 B123123 C12312312" ); //should be trimmed and in order
    stringstream ss(input);
    char c;
    int data;
    while (ss >> c >> data)
        std::cout << c << " " << data << std::endl;