I am trying to do a basic program that reads from a txt file and ouputs the results in a specific order on the screen. Its a salary type program.

Here is my salary.txt file

Randall  Jones     25645.88 1999
Jim      Johnson   56709    1992
Carol    Troth     42387.05 2000
Briannal Deweyage  12567.99 2004
Bob      Hoskins   76534.00 2002
Sonny    Thomas    100000   1983
Susan    Sarandon  1645.21  1981
Jamie    Trance    44765.   1997
Kellie   Whisk     54321.00 1993
Katie    Turnerty  99.99    1979

Here is my salary.cpp file

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;
int main()
{
        ifstream myFile;
        double salary, totalSalary=0;
        string firstName, lastName, yearHired, fullName;

        myFile.open("salary.txt");
        cout<<fixed<<showpoint<<setprecision(2);
        myFile>>firstName>>lastName;

        while(!myFile.eof())
        {
                myFile>>firstName>>lastName>>salary>>yearHired;
                fullName = lastName + ", " + firstName;
                cout<<setw(10)<<fullName<<setw(10)<<salary<<setw(10)<<yearHired<<endl;
                totalSalary += salary;
                myFile>>firstName>>lastName>>salary>>yearHired;

        }

        cout<<endl<<setw(10)<<setfill('=')<<totalSalary<<endl;

        if(salary >= 50000)
        {
                cout<<"*";
        }

        myFile.close();
        return 0;

}

I am trying to get the results of putting those two programs together to output this screen:


Name Salary Hired
===============================
Jones, Randall $ 25645.88 1999
Johnson, Jim $ 56709.00 1992*
Troth, Carol $ 42387.05 2000
Deweyage, Briannal $ 12567.99 2004
Hoskins, Bob $ 76534.00 2002*
Thomas, Sonny $100000.00 1983*
Sarandon, Susan $ 1645.21 1981
Trance, Jamie $ 44765.00 1997
Whisk, Kellie $ 54321.00 1993*
Turnerty, Katie $ 99.99 1979
===============================
Total Salary ********* $414675.12

In summary, what im trying to do is to implement the program and output the lastName first, then the firstName... then have all of the salary numbers line up according to the decimal point with two decimal points to the right...and output all the years of the dates hired. then at the end of the program, add all of the salaries together, and output the final results. It seems like a relatively easy program, and my program compiles, but it shows this:
1999, 25645.88-71319654623666479083774548546739424319273737775962062297671976791828718398891937412147005776399452111478761517538510862556217750430449474153312582057515939921969392187159892407934170908216628314512097129276284689494211281475324122213783290655497100314044815979566684971355260584574063554057855540473626624.00
1999, 25645.88-71319654623666479083774548546739424319273737775962062297671976791828718398891937412147005776399452111478761517538510862556217750430449474153312582057515939921969392187159892407934170908216628314512097129276284689494211281475324122213783290655497100314044815979566684971355260584574063554057855540473626624.00
1999, 25645.88-71319654623666479083774548546739424319273737775962062297671976791828718398891937412147005776399452111478761517538510862556217750430449474153312582057515939921969392187159892407934170908216628314512097129276284689494211281475324122213783290655497100314044815979566684971355260584574063554057855540473626624.00
1999, 25645.88-71319654623666479083774548546739424319273737775962062297671976791828718398891937412147005776399452111478761517538510862556217750430449474153312582057515939921969392187159892407934170908216628314512097129276284689494211281475324122213783290655497100314044815979566684971355260584574063554057855540473626624.00
and those numbers keep going on and on...

It seems to be stuck on the first numbers given in my salary.txt file, and i have spent about 2 hours plugging in new things and taking things out, but still with the same result.

thanks for all your help

so pretty much you want to display the fist name last and vice versa and then calculate the total salary?

yes, very simple program with few calculations, just a sense of putting things in the right place and calculating the total salary.. I'm just stuck haha, I'm 100% new to C++ though so that deserves a little slack i guess haha

A major problem is how you are reading in data and controlling the loop. You read in a first and last name, then first off in your loop read first and last name again. So your input is out of sync with the actual data.

When using eof( ) to test for end of data, you should read just one item outside the loop, test, and read the rest of the line inside the loop. At end of loop, read in just that one item again. This lets you know that another line of data exists.

This method is suitable when you can be assured of a correct file, and that file ends with a newline.

So, to fix what you have:

myFile >> firstName;

        while( !myFile.eof() )
        {
                myFile >> lastName >> salary >> yearHired;
                fullName = lastName + ", " + firstName;
                cout << setw(25) << fullName << setw(10) << salary << setw(10) 
                       << yearHired << endl;
                totalSalary += salary;
                myFile >> firstName;

        }

And don't be afraid of a little whitespace between operators and their operands!

The setw( ) for the full name should reserve more space, like the 25 I used above.

Val

thank you very much for clearing that up! Now my program reads fine, but I still have a little problem I need help with to clean cut the finished product. I like mine to be neat haha.


Here is my code:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;
int main()
{
        ifstream myFile;
        double salary, totalSalary=0;
        string firstName, lastName, yearHired, fullName;

        myFile.open("salary.txt");
        cout<<"Name            Salary        Hired"<<endl;
        cout<<"==================================="<<endl;
        cout<<fixed<<showpoint<<setprecision(2);
        myFile>>firstName;

        while(!myFile.eof())
        {
                myFile>>lastName>>salary>>yearHired;
                fullName = lastName + ", " + firstName;
                cout<<setw(10)<<fullName<<setw(10)<<salary<<setw(10)<<yearHired<<endl;
                totalSalary += salary;
                myFile>>firstName;

                if(salary >= 50000)
                {
                        cout<<"*";
                }
        }
        cout<<"========================================";
        cout<<endl<<"Total Salary     "<<setw(10)<<setfill('*')<<totalSalary<<endl;

        myFile.close();
        return 0;

}

and here is what my output is:

Name            Salary        Hired
===================================
Jones, Randall  25645.88      1999
Johnson, Jim  56709.00      1992
*Troth, Carol  42387.05      2000
Deweyage, Briannal  12567.99      2004
Hoskins, Bob  76534.00      2002
*Thomas, Sonny 100000.00      1983
*Sarandon, Susan   1645.21      1981
Trance, Jamie  44765.00      1997
Whisk, Kellie  54321.00      1993
*Turnerty, Katie     99.99      1979
========================================
Total Salary     *414675.12

Now I am trying to put the asterisk's on the right side of the output, and at the same time, I would like to line up the salary numbers in a straight line according to the decimal point, and the yearHired numbers in a straight line. How do I right and left justify the numbers?

thanks!!!!!!!!!!!!!!!!!!!!

for lineing up the numbers:

use the fixed and setprecision(x) options where x is the number of decimal places you want to use. This needs to be declared before the printout.

( ie : cout << fixed << setprecision(2); )

and like was mentioned before use the setw(x) to help align the columns.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.