Reading a text file (Help)

I have an asignment where i must read and then later do calculations with the data.

I need to

•Code for employee classification (H for hourly, S for salaried)
•If hourly, two fields follow: hours worked and hourly pay rate
•If salaried, only one field follows: annual salary

This is the data to be read:

101456 H 20 6.57
100045 S 81994.12
100321 H 45 23.50
101987 H 39 15.76
100486 S 116935.65
100357 H 50 18.19
102003 H 30 12.75
123887 S 226345.43
110441 S 54783.28
101119 H 35 22.22
114532 S 152573.23
100003 H 40 19.00
101285 H 60 29.95
105539 S 100284.54
176531 S 78546.77
101119 H 38 43.15

This is what I have but I can't seem to get it to work. Any help would be greatly appreciated .

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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
//Declare variables
int empid, hours;
char code;
double salary, rate, gp;

ifstream inFile;
inFile.open ("F:\\paydata.txt");

if (!inFile) 
{
    cout << "Unable to open file " << endl;
    exit(1);   // call system to stop
}


while(!inFile.eof())
if (code == 'H')
{
	inFile >> empid >> code >> hours >> rate;  
}
else if(code == 'S')
{
inFile >> empid >> code >> salary;
}


return 0;
}


I believe I am not initializing the variables correctly from the text file as I keep getting those types of errors
You need brackets around your while loop, you should call inFile.good() in place of !inFile.eof(), you never read in the variable code. Read it in first, then check for its value.
Topic archived. No new replies allowed.