methods for sorting file input of a mixed type into usable data

Hello,

I'm nearing the end of my first semester in C++ and I've pretty well covered the entire text (starting out with c++ 6th ed, by Gaddis)

However, my last lab covers a seemingly arcane subject not well covered in the text:
differing sequential file input for input to an array. The input file format is:
Exam01:80
Lab01:80
etc,

and I want to get the grades into an array of floats.

Searching online I see a lot of info on moving the file read pointer or making all output except numbers into white space characters, but all of these answers come with indigestible code.

I can copy paste just fine, but really there is no point if I do not understand it.

Perhaps somebody here is willing to point me to a guide or lay out some good concepts. I have a fair amount of time left to research and code this, so I don't need a functioning coded answer as much as advice.

Thanks,
Josh.

Last edited on
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.
Last edited on
Topic archived. No new replies allowed.