Matthew Blake
Matthew Blake

Reputation: 1

Updating variables using stringstream

I'm new here and very new to writing code.

I'm writing a function using stringstream that would take in an input_string (input_string is read from what I'm assuming is a file that came with my starter code, but its labeled as a ".in" file) and update 3 variables based on the integers that are found in the input string.

When I used my test case, its reading the input string just fine (I ended up putting a cout << ss.str() << endl; statement in my function just to make sure it was reading the input from my test case), but its not updating the 3 variables that I would like for it to update - currentFloor, targetFloor, and angerLevel.

I'm not asking for people to do my homework, but more of a better explanation that can help me expand my understanding of stringstream and updating variables.

Person::Person(string input_string) : Person() {
    string str(input_string);
    stringstream ss(str);
    int x;
    ss >> x;
    string s;
    ss >> s;
    cout << ss.str() << endl; //this cout statement is something I put there to check to see if its reading the input_string with a test case
}

My test case

0f0t10a3 //what was printed using the cout statement
0 0 Expected 10 3 // suppose to update targetFloor and angerLevel
0 Expected 0 //this works
0 1 Expected 5 6
0 1 Expected 5 6
0 Expected 1

Upvotes: 0

Views: 239

Answers (2)

Tony Delroy
Tony Delroy

Reputation: 106126

The code below has comments to explain:

Person::Person(string input_string) : Person() {
    // create an input stringstream to help you parse input_string
    std::istringstream ss(input_string);

    // some variables to parse values into...
    int x;
    string s;

    if (ss >> x >> s)
        std::cout << "read x=" << x << " and s='" << s << "'\n";
    else
        std::cerr << "unable to parse an int and space-delimited word from '"
            << input_string << "'\n";
}

If that doesn't extract the values you expect, the problem will be in the input_string value, which you'll see printed in the error message. If you need help getting the right input_string from your input file, then try something like below and search for Q&A on ifstreams to sort yourself out; ask a new question here if stuck...

if (std::ifstream in{"some_filename.in"}) {
    std::string input_string;
    while (getline(in, input_string)) {
        Person person{input_string};
        // do something with each person parsed...
    }
}
else
    std::cerr << "unable to open input file\n";

Upvotes: 0

Loki Astari
Loki Astari

Reputation: 264471

You can test if the reading from the stream worked:

    int x;
    if (ss >> x) {
        std::cout << "Correctly read into X: " << x << "\n";
    }
    else {
        std::cout << "Read from ss into X failed.\n"
                  << "Note any subsequent reads will also fail unless"
                  << "you reset the error flag on ss\n";

        ss.clear(); // This will reset all the flags on the stream.

        std::cout << "Check what is on ss that failed\n"
        std::cout << "Bad SS: " << ss.str() << "\n"
   }

Upvotes: 1

Related Questions