Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with re-using a stringstream object

Tags:

c++

windows

I'm trying to use safe practices in handling input with numbers only in C++, so I use a stringstream object as so:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    int first, second;
    string input;

    stringstream sstream;

    cout << "First integer: ";
    getline(cin, input);
    sstream.str(input);
    sstream >> first;

    cout << first << endl; //display user input in integers

    cout << "Second integer: ";
    getline(cin, input);
    sstream.str(input);
    sstream >> second;

    cout << second << endl; //display user input in integers

    getline(cin, input); //pause program

    return 0;
}

However, the second time around it seems to give the variable 'second' an arbitrary value. This is the output:

First integer: 1
1
Second integer: 2
2293592

If I declare two stringstream objects and use them respectively for both variables it seems to work fine. Does this mean that I cannot re-use a stringstream object in the way I'm trying to do? In my real program I intend to handle much more than two input values from the user, so I just want to make sure if there's another way instead of making multiple stringstream objects. I doubt it's of great relevance but I'm on Windows XP and I'm using MinGW as my compiler.

I greatly appreciate any help.

like image 371
kaykun Avatar asked Jan 27 '10 02:01

kaykun


2 Answers

Use sstream.clear(); after sstream >> first;.

like image 172
Max Shawabkeh Avatar answered Nov 02 '22 11:11

Max Shawabkeh


You need to reset the state of the stringstream. Generally, this involves two steps: clearing the buffer:

sstream.str("");

and resetting the error state flags:

sstream.clear();

If you don't clear the buffer, if you get an input like "123abc" then "abc" will still be in the stream when you try to read from it the next time.

You should also make sure to test the fail state of the stream (sstream.fail()) to ensure that the extraction was successful. If you want to be sure that the user only entered an integer (i.e., you want to prevent the user from inputting, say, "123abc", then you should test to make sure sstream.eof() is true.

like image 30
James McNellis Avatar answered Nov 02 '22 10:11

James McNellis