ive never done this myself, and am actually working on a final project for my first C++ class aswell.
However i can tell you the direction id go.
First id do the whole read the line into a string like normal. so like
for(file_in>>my_string){
and than at that point i would try to convert the string into an array full of characters. Or perhaps a vector since you can resize those on the fly you could make sure the vector is only as big as the line your getting from the file.
I would than loop through the character array(or vector), and do something like this(in pseudo code). Also note that im kindof assuming there's some form of a try and catch statement in C++. At-least in java there's a try and catch thing which allows that tells the code to try to run a certain block of code, if an error occurs than execute the catch statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
bool convert = false;
int test_int;
int converted_count = 0;
char numbers[10];
char non_numbers[10]
int cnvrt_count;//keeps track of the index for adding number characters to the //numbers array
int count;//keeps track of the index for adding non number characters to that array
for(int i = 0; i<array_size; i++){
char character = character_array[i];//the array of characters that came //from the string
try{
character>>test_int;
convert = true
}
catch{
cout<< character << " cant be converted to an int \n";
convert = false;
if(convert == true;){
numbers[cnvrt_count] = test_int;
cnvrt_count++;}
else{
if(character_array[i] != '.'){
non_numbers[count[ = character_array[i];
count++;}
else{
numbers[cnvrt_count] = character_array[i];
cnvrt_count++;}
}
}
|
basically,
if character from character array that was converted from the input string can be converted to an int than set bool convert to true. else check if the character is a decimal. If it is a decimal add it to the number array if its not add the character to the non_number array.
after this you would want to try to convert the 2 character arrays into strings. I think you can do that like this.
string number(numbers);//numbers is the character array
and than you can convert that string into an double.
anyway thats my theory.