Looping until EOF

Why is my output like this instead of all the data being in the table?
thanks

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    
    ifstream    fin;
    ofstream    fout;
    string      Name;
    int         Id;
    float       grossIncome ;
    float       netSalary;
    float       incomeTax =0;

    
    cout <<"\nEnter the name and location of the input file:   " << endl;
    string file_input;
    getline(cin, file_input);
    fin.open(file_input.c_str() ) ;
    
    cout <<"Enter the name and location of the output file:   ";
    string file_output;
    getline(cin, file_output);
    fout.open( file_output.c_str() );
    
    getline(fin,Name);
    while(!fin.eof())
    {
        fin >> Id >> grossIncome;
        cout << setw(20) << left  << Name
        << setw(8)  << right << Id
        << setw(10) << grossIncome  << endl;
        fin.ignore(10,'\n');
        getline(fin,Name);
  
    }
    
    if (grossIncome <= 3500)
    {
        incomeTax = 0.00;
    }
    else if (grossIncome >= 3500 && grossIncome <= 8000)
    {
        incomeTax = 0 + 0.06 * (grossIncome - 3500);
    }
    else if (grossIncome >= 8000 && grossIncome <= 20000)
    {
        incomeTax = 270.00 + 0.11 * (grossIncome - 8000);
    }
    else if (grossIncome >= 20000 && grossIncome <= 34000)
    {
        incomeTax = 1590.00 + 0.17 * (grossIncome - 20000);
    }
    else if (grossIncome >= 34000 && grossIncome <= 54000)
    {
        incomeTax = 3970.00 + 0.24 * ( grossIncome - 34000);
    }
    else if (grossIncome >= 54000)
    {
        incomeTax = 8770.00 + 0.32 * ( grossIncome - 52000);
    }
    else if (grossIncome < 0)
    {
        cout << "****Invalid Income";
    }
    
    netSalary = grossIncome - incomeTax;
    
    cout << setfill(' ') << left << setw(18) << "\tName";
    cout << setfill(' ') << left << setw(12) << "ID";
    cout << setfill(' ') << left << setw(17) << "Gross Income";
    cout << setfill(' ') << left << setw(12) << "Taxes";
    cout << setfill(' ') << left << setw(16) << "Net Income";
    cout << endl;
    
    cout << setfill('=') << setw(70)<<"\t";
    cout<<endl;
    
    cout << setprecision(2) << showpoint << fixed;
    cout << setfill(' ') << "\t" << setw(17)<< Name;
    cout << setfill(' ') << setw(12) << Id;
    cout << '$' << setfill(' ') << setw(16) << grossIncome;
    cout << '$' << setfill(' ') << setw(11) << incomeTax;
    cout << '$' << setfill(' ') << setw(16) << netSalary;
    cout << endl;
    
}


Enter the name and location of the input file:   
/Users/MJ/Desktop/incomes.txt
Enter the name and location of the output file:   /Users/MJ/Desktop/incomfdfdf.txt
Miss Informed         125432     32560
Sweet Tooth             5432      9500
Bad Data                1255     -4500
John Smith              1225      3500
Nancy Brown             1555    154500
	Name             ID          Gross Income     Taxes       Net Income      
	=====================================================================
	Nancy Brown      1555        $154500.00       $41570.00   $112930.00 


Sample file

Miss Informed
125432 32560.0
Sweet Tooth
5432 9500
Bad Data
1255 -4500.0
John Smith
1225 3500.0
Nancy Brown
1555 154500.00
You should not loop until EOF. Instead, you should loop until input fails.
31
32
33
34
35
36
37
38
39
40
    while(/*!fin.eof()*/ fin >> Id >> grossIncome && fin.ignore(10, '\n') && getline(fin, Name))
    {
//        fin >> Id >> grossIncome;
        cout << setw(20) << left  << Name
        << setw(8)  << right << Id
        << setw(10) << grossIncome  << endl;
//        fin.ignore(10,'\n');
//        getline(fin,Name);
  
    }
I am required to use while(!fin.eof())
I tried your suggestion and it didn't work with me but I understand why you didn't use eof but I am required to use it
Ask the teacher what you are actually supposed to learn by having to work around questionable compulsory constructs.
You have the variables to store the information for one record. You read all records (into the same set of variables,) and only process the last one (which is the last to occupy the set of variables.) Change how you do things.
Last edited on
Topic archived. No new replies allowed.