I'm trying to read 3 string
separately from user input, it all contains numbers separated by white spaces for example "1 2 3". I will read in three lines and store these number in a vector of integer
However the program stop reading the string
after I enter "1 2 3"
I'm expecting to enter "1 2 3" first, then enter "4 5 7", then " 8 9 0", and add all these number to the vector.
the print out look like this
Enter the puzzle
1 2 3
123
I'm expecting something like this
Enter the puzzle
1 2 3
4 5 6
7 8 9
1234567890
Where could the problem be? I tried the following
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <sstream>
using namespace std;
int main(){
vector<int> arr;
cout << "Enter the puzzle" << endl;
string line1;
string line2;
string line3;
cin >> line1;
cin >> line2;
cin >> line3;
istringstream is(line1);
int num;
while(is>>num){
arr.push_back(num);
}
istringstream is2(line2);
while(is2>>num){
arr.push_back(num);
}
istringstream is3(line3);
while(is3>>num){
arr.push_back(num);
}
for(int i = 0; i < arr.size(); i++){
cout << arr[i];
}
return 0;
}
Does the problem exist because of isstringstream
?