How do I split a string by space and change the data type of the string?

Viewed 48

I have some arrays. I have to use parallel arrays, I cannot use vectors. I will check the items before they go into the arrays.

Input example:

23.6 Felix 45

double timeArray[size]
int playernumberArray[size]
string lastnameArray[size]

string = input;    
cin >> input;

How do I split up the input, change the data type, and put it into the appropriate array?

1 Answers

iostreams' formatted inputter (operator >>) is designed to parse whitespace delimited text. If you have input where the distinction between different kinds of whitespace is important (the difference between space, tab, and newline), then it is generally not what you want.

Instead you can read lines (usually with getline) and then break the resulting strings in to tokens, either using string::find or regexps, or even stringstream and operator >>.

Related