vector - C++ read in a line of string as ints? -
i trying read line of string characters numbers (e.g "30 40 50 20") , put them vector. need avoid empty space , newlines. when read input, doesn't see string "30", sees characters "3" , "4".
void input() { getline(cin,line, '\n'); (int = 0; < line.length(); i++) { if (! (isspace(line[i]))) { cout << line[i] << ", "; scores.push_back(line[i]);//(atoi(input)); } } cout << scores.size() << "! "; }
a line "30 40 50" won't give vector size of 3, give size of 6. optimal ways around issue?
edit: should have clarified in original message challenge, in unable include string stream library in original case.
i think you're doing right thing grabbing whole line before parsing, otherwise bit of pickle. have parsing. right you're pulling out individual characters.
the following isn't optimal it'll started — continue using formatted stream extraction, isolated line file.
so:
void input() { getline(cin, line, '\n'); istringstream ss(line); int val; while (ss >> val) scores.push_back(val); cout << scores.size() << "! "; }
Comments
Post a Comment