I tried using cin.getline()
, but it only works with char
arrays.
I want to get an input of numbers with one char
at the end (Q
), with spaces in between all characters/numbers, but not include the whitespace in the array itself when indexing.
Input:
Enter integers (Q to quit): 1 2 1 8 8 9 Q
Output:
Second smallest: 2
CodePudding user response:
getline()
is not the right solution for this task. Use operator>>
instead, eg:
int value;
while (cin >> value)
{
// use value as needed...
}
CodePudding user response:
Try using std::getline
and std::istringstream
:
std::string text_line;
std::istringstream number_stream(text_line);
int number = 0;
while (number_stream >> number)
{
std::cout << number << "\n";
}
This technique is often used when parsing text lines of numbers.