What do you know about the data?
You could read the text as a string and then see whether it's a double or an int (and accordingly list them), if neither then you can ignore it. Can't use stringstream in this case because both int and double will happily accept your values.
I don't know if there is a standard library function for checking if a value is float. If not you can write your own function.
Stringstream, is that just to take the value to be able to put into any data type such as the int or string. |
You can say that yes.
and the extraction operator >> , is this basically saying move on to next word, line or add to existing. |
It's the same extraction operator you use for cin>>var; It tells to move data from cin to var.
I was thinking about maybe doing something like this:
1 2
|
cin >> num;
cin.get(ch);
|
Where num will catch integers and cin.get() would take decimal point but I realize that it will make it unnecessarily complicated.
Yeah it's really very unnecessarily complicated.
How it would have looked like to use that method (but I didn't test):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
int integer_part;
double decimal_part;
char decimal_point;
cin >> integer_part;
cin >> decimal_point;
if(!cin) {
cin.clear();
cin.ignore(INT_MAX, ' ');
}
else if(decimal_point != '.') {
putback(decimal_point);
integer_array.push_back(integer_part);
}
else {
cin >> decimal_part;
while((int)decimal_part != 0)
decimal_part /= 10;
integer_part += decimal_part;
floating_array.push_back(integer_part);
}
|
And also don't use this method because it relied on floating arithmetic as I just realized too.
So you're better off with a function that uses a for-loop to check for decimal points and verifies all other indexes are digits (and also makes sure there's only one decimal point).
Unless there's a standard library function then you could just use that.