Hi! i need help writing a program to read in a file to my program. the file looks like this
12
283 20.00 5
156 50.00 20
385 15.00 20
739 100.00 20
555 50.00 50
412 100.00 1
904 150.00 8
716 75.00 3
872 35.00 70
202 20.00 50
194 200.00 10
810 40.00 4
im using fstream to to open it. I have been trying to use for loops to read it, but i cant get it to work properly. the first column is employee data and the second is how much they earn and the last is how much overtime. Please help!
I'm guessing that 12 is the amount of employees there are. You can make the code a little more streamlined using pointers, but this will probably suffice:
(overtime is an int?)
1 2 3 4 5 6 7 8 9 10 11
constint SIZE = 100;
int amount, data[SIZE], overtime[SIZE];
double wage[SIZE];
ifstream inFile;
// open the file
inFile >> amount;
for (int i = 0; i < amount; i++){
inFile >> data[i] >> wage[i] >> overtime[i];
}
Those extraction operators look backwards, haven't quite put that into muscle memory...
remember to close the file, along with actually opening the file.
infile.open("FILE LOCATION HERE");
infile.close();
by the way, wages was never actually declared in that last segment of text, if you were just coping it.
thanks alot! the problem is. 12 is the amount of lines to be read. like for the first number im trying to say there are 283 employees making 20.00 with 5 hrs of overtime.
thanks alot! the problem is. 12 is the amount of lines to be read. like for the first number im trying to say there are 283 employees making 20.00 with 5 hrs of overtime.
You don't need to worry about line breaks for this, the extractor will take care of this for you. Just start the extraction like Subzero030201 or I have: inFile >> amount. Then you can loop that amount of times.