Stuck! Weird problem with input file!

Okay, trying to do a simple homework problem.....writing a C++ program to read a text file and output the results.
Here is C++;
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream.h>
#include<fstream.h>

//PAYROLL SYSTEM PHASE 3B Interactive Loop w/ Input File

int main(){

       
       int numberofEMPLOYEES;     //define the value as an interger
       int employeeID, hoursWORKED;
       float hourlyRATE, grossPAY, taxAMOUNT, netPAY; 
       float const taxRATE=(float).10;
       numberofEMPLOYEES = 1;
              
       ifstream inFile;
       inFile.open("employee.in");
                    
       while(inFile>>employeeID>>hoursWORKED>>hourlyRATE) {
       
       cout<<"Enter Employee ID:";
       inFile>>employeeID;
       cout<<"Enter the Hours Worked:";
       inFile>>hoursWORKED;
       cout<<"Enter the Hourly Rate:";
       inFile>>hourlyRATE;
       grossPAY=hoursWORKED*hourlyRATE;
       taxAMOUNT=taxRATE*grossPAY;
       netPAY=grossPAY-taxAMOUNT;
       cout<<"Your Employee ID is "<<employeeID<<endl;
       cout<<"Your Hours Worked are "<<hoursWORKED<<endl;
       cout<<"Your Hourly Rate is "<<hourlyRATE<<endl;
       cout<<"Your Gross Pay is "<<grossPAY<<endl;
       cout<<"Your Tax Rate is "<<taxRATE<<endl;
       cout<<"Your Tax Amount is "<<taxAMOUNT<<endl;
       cout<<"Your Net Pay is "<<netPAY<<endl;
       
       numberofEMPLOYEES = numberofEMPLOYEES + 1;
       
       
       }
       inFile.close();
       
      
      system("PAUSE"); 
       
              return 0;
    
     }//MAIN 


Then I put in my employees.in file
1234 (tab) 40 (tab) 40.00
and it works PERFECTLY!!!

But I then go and add more data on the next line of my employees.in file so it looks like:

1234 (tab) 40 (tab) 40.00
4567 (tab) 30 (tab) 30.00

When I re-run my program it only gives me the new data! the first line is missing! If I add another line of data it again, skips the first line!

This is driving me to drink! Can someone please help and let me know what I am doing wrong?

Thanks in advance for saving my sanity!
It is because you are getting input twice per loop.

The first time it seemed to work because the second attempts to get data failed, and did not change your variables.

Try a file with, say, ten employees. You'll see the data for every second one.


To fix it, get rid of lines 20 through 25. (You don't need them.)

Hope this helps.
Perfect Thanks! I just tried it and you are right~ Thank you~
Topic archived. No new replies allowed.