I have a question regarding my code. I would like to obtain information from a text file and assign that information into class objects. I will use the information from objects later on to write a program.
My text file looks like this:
984763 A 20 18
762613 A 19 17
587123 A 22 16
897654 D 85 19
where the first columns represent ID#, Type, processing time 1, processing time 2.
cout<< ID << endl;
cout<< axle_pt << endl;
cout<< Type << endl;
cout<< axle_pt << endl;
} ]
The output gives me the first element in the text file (984763) and nothing for the rest. It seems that the file is opening but there is something wrong with my code. Any help please?
Thank you Thomas1965. I guess what I am trying to do is not to just read the text file, I would also want to store it into class objects. This is where I face a difficulty the most.
Thank you 5a8Ym39o6. I receive an error when I write your code. I guess the problem with the code you provided is that I have to declare the array size while it should be declared based on the text file each time the code is run. Also, I have the following variable in private:
ID, Type, feeder_pt, axle_pt which the program cannot access on main.
Thank you so much Thomas1965. Do I do that in public or private? Also, do I need to write void before truck? Also, do I keep the rest of the code the same?
Sorry I know it might be a stupid question that I am asking but I am just a beginner.
Thank you so much 5a8Ym39o6 for your edit. I just get a lot of errors when I run the code because it seems that all the variables are declared in private which main could not access. Also, where do I define the array trucks [5]? or is it the class that I already defined?
> Also, where do I define the array trucks [5]? or is it the class that I already defined?
It is in the place of where it is (in my code). No need to change its original place.
Thank you 5a8Ym39o6 but the code still has a lot of errors even after declaring the variables in public. It says that the variables (i.e ID, Type, axle_pt, feeder_pt) are not declared in this scope. Also, the IF statements in the code give me errors so I run the program without the if statements.
No that really doesn't help, you now have the possibility of accessing the array out of bounds if your file contains more than 800 records. The real answer would be to use std::vector instead of the array and push the records into the vector as you go letting the vector resize as necessary to hold the contents of the file.