help with reading a file

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!
Last edited on
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
const int 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.
Last edited on
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.
Here is another way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream inFile;
int amount;
inFile.open("file.txt");
inFile >> amount;
int *data = new int[amount];
int *overtime = new int[amount];
double *wage = new double[amount];
for (int i = 0; i < amount; i++)
{
	  inFile >> data[i] >> wage[i] >> overtime[i];
		  cout <<"|Data : "<<data[i] <<" | Wage : "<< wage[i] <<" | Overtime Hours: "<< overtime[i]<<" |"<<endl;
	  cout <<"-------------------------------------------------------------------------"<<endl;
}
delete[]data;
delete[]overtime;
delete[]wage;
return 0;
}

or
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
#include <fstream>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
double dtemp;
int itemp;
vector<int> data;
vector<int> overtime;
vector<double> wage;
ifstream inFile;
int amount;
inFile.open("file.txt");
inFile >> amount;
for (int i = 0; i < amount; i++)
{
	inFile>> itemp;
	data.push_back(itemp);
	inFile>> dtemp;
	wage.push_back(dtemp);
	inFile>> itemp;
	overtime.push_back(itemp);
		  cout <<"|Data : "<<data[i] <<" | Wage : "<< wage[i] <<" | Overtime Hours: "<< overtime[i]<<" |"<<endl;
	  cout <<"-------------------------------------------------------------------------"<<endl;
}

return 0;
}

or
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
#include <fstream>
#include <iostream>
using namespace std;
struct info
{
int data;
int overtime;
double wage;


};
int main()
{
ifstream inFile;
int amount;
inFile.open("file.txt");
inFile >> amount;
struct info *sinfo = new info[amount];

for (int i = 0; i < amount; i++)
{
	  inFile >> sinfo[i].data >> sinfo[i].wage >> sinfo[i].overtime;
		  cout <<"|Data : "<<sinfo[i].data <<" | Wage : "<< sinfo[i].wage <<" | Overtime Hours: "<< sinfo[i].overtime<<" |"<<endl;
	  cout <<"-------------------------------------------------------------------------"<<endl;
}
delete [] sinfo;
return 0;
}
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.