I'm currently trying to solve a programming assignment and i got the logic of it, however i find it hard to implement.
What i need to do basically is fill an array with objects. Each object is a class that contains only one type of data. This means i can place int, double and string for example in one simple array.
However i can't figure out how to read data and then decide what it is. Even if i use templates once i call the function i have to give it a type, so getType<int> for example will not work with double or string.
I know about typeID and how to use it, i just can't figure out where to use it.
You have to read it into a string, then examine it to see if it's an integer, double, or just a string. An integer would contain only numbers, a double would contain only numbers and a single period, and a string would result if those two tests failed.
Yeah, my suggestion would also be to read your input into an std::string object first, then you can manipulate it to find out what it is.
I don't think there's any standard functions to tell what a string type can be read as but there are standard function for character. And since a string is just a load of characters you could iterate through.
i.e.
get input sting,
loop while you're not at end of string and while there's not conflict of types
if character is a letter it's clear that you'r looking at a char.
if it's a number then move through the characters till either you hit a non-numerical number/'.' character/end of string
if '.' is found while doing this then you have float/double.
if you hit end of string or a conflict first then take this to be a char/short/int