Problem with re-using a stringstream object

前端 未结 4 1636
孤独总比滥情好
孤独总比滥情好 2021-02-08 18:43

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

#include 
#include 

        
4条回答
  •  别那么骄傲
    2021-02-08 19:27

    cout << "First integer: ";
    getline(cin, input);
    sstream.str(input);
    sstream >> first;     // state of sstream may be eof
    
    cout << "Second integer: ";
    getline(cin, input);
    sstream.str(input);
    sstream.clear();      // clear eof state
    sstream >> second;    // input from sstream
    

提交回复
热议问题