Why is my output like this instead of all the data being in the table? (PS : must use while(!fin.eof())
thanks

             #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/output.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

Edited 3 Years Ago by refphlex

From line 38 to line 85 should be in a loop. Since only Nancy Brown is added to the table well that indicates the last bit of information read was of Nancy Brown so that is why it is only outputted.

So basically the loop has to read information about one particular person, then put it in the table straight away.

Also since you're outputting the info twice you might need to store the results in the first loop into a vector and use a second loop to display the table. A class would work well here. Consider the following:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Person
{
public:
    string Name = "";
    int ID = 0;
    double GrossIncome = 0;
    Person()
    {
    }
    Person(string _name, int _id, double _grossincome)
    {
        Name = _name;
        ID = _id;
        GrossIncome = _grossincome;
    }
    double IncomeTax(double gincome)
    {
        return (gincome >= 54000 ? 8770.00 + 0.32 * (gincome - 52000) :
            gincome >= 34000 ? 3970.00 + 0.24 * (gincome - 34000) :
            gincome >= 20000 ? 1590.00 + 0.17 * (gincome - 20000) :
            gincome >= 8000 ? 270.00 + 0.11 * (gincome - 8000) :
            gincome >= 3500 ? 0.06 * (gincome - 3500) : 0);
    }
    double NetIncome(double gincome, double tax)
    {
        return gincome - tax;
    }

};
int main()
{
    ifstream fin;
    ofstream fout;
    vector<Person> AllPersons;
    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());

    while (!fin.eof())
    {
        Person temp;
        getline(fin, temp.Name);
        fin >> temp.ID >> temp.GrossIncome;
        cout << setw(20) << left << temp.Name
            << setw(8) << right << temp.ID
            << setw(10) << temp.GrossIncome << endl;
        fin.ignore(10, '\n');
        AllPersons.push_back(temp);
    }
    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;
    for each (Person var in AllPersons)
    {
        cout << setfill('=') << setw(70) << "\t";
        cout << endl;
        cout << setprecision(2) << showpoint << fixed;
        cout << setfill(' ') << "\t" << setw(17) << var.Name;
        cout << setfill(' ') << setw(12) << var.ID;
        cout << '$' << setfill(' ') << setw(16) << var.GrossIncome;
        double tax = var.IncomeTax(var.GrossIncome);
        cout << '$' << setfill(' ') << setw(11) << tax;
        cout << '$' << setfill(' ') << setw(16) << var.NetIncome(var.GrossIncome, tax);
        cout << endl;
    }
    system("pause");
}

Edited 3 Years Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.