-2
#include<iostream>
#include<string>
#include<fstream>
#include<vector>


using namespace std;
void check(ifstream &iFile)
{
    if (!iFile.is_open())
    {
        cout << "Data file not found!" << endl;
        system("pause");
        exit(1); // exit the program if the file is not found.
    }
}

void readIn(ifstream &iFile, vector<string> &fName, vector<string> &lName, vector<string> &jTitle, vector<string> &eID, vector<double> &hoursWorked, vector<double> &wage, vector<int> &deductions, vector<double> &sPay, string sTemp, double dTemp, int iTemp)
{
    while (!iFile.eof())
    {
        iFile >> sTemp;
        fName.push_back(sTemp);

        iFile >> sTemp;
        lName.push_back(sTemp);

        iFile.ignore();
        getline(iFile, sTemp);
        jTitle.push_back(sTemp);

        iFile >> sTemp;
        eID.push_back(sTemp);

        iFile >> dTemp;
        hoursWorked.push_back(dTemp);

        iFile >> dTemp;
        wage.push_back(dTemp);

        iFile >> iTemp;
        deductions.push_back(iTemp);

        iFile >> dTemp;
        sPay.push_back(dTemp);

    }
    cout << "completed" << endl;

}

int main()
{
    ifstream iFile;
    iFile.open("data.txt");
    check(iFile);

    vector<string> fName, lName, eID, eStatus, jTitle;
    vector<double> nPay, gPay, oPay, oHours;
    vector<double> hoursWorked, wage, sPay;
    vector<int> deductions;

    // temporary names to pass to the vector
    string sTemp; // string temp
    double dTemp=0; // double temp
    int iTemp=0; // integar temp
    readIn(iFile, fName, lName, jTitle, eID, hoursWorked, wage, deductions, sPay, sTemp, dTemp, iTemp);
/*  while (!iFile.eof())
    {
        iFile >> sTemp;
        fName.push_back(sTemp);

        iFile >> sTemp;
        lName.push_back(sTemp);

        iFile.ignore();
        getline(iFile, sTemp);
        jTitle.push_back(sTemp);

        iFile >> sTemp;
        eID.push_back(sTemp);

        iFile >> dTemp;
        hoursWorked.push_back(dTemp);

        iFile >> dTemp;
        wage.push_back(dTemp);

        iFile >> iTemp;
        deductions.push_back(iTemp);

        iFile >> dTemp;
        sPay.push_back(dTemp); 
    }*/

    int sizeOf = fName.size();
    for (int a = 0; a < sizeOf; a++)
    {
        cout << fName.size() << " FName " << fName[a] << " LName " << lName[a] << " JobTitle " << jTitle[a] << endl;
        cout << "EmployeeID " << eID[a] << " Hours Worked " << hoursWorked[a] << " Hourly Wage " << wage[a] << endl;
        cout << "Deductions " << deductions[a] << " Salary Pay " << sPay[a] << endl;
    }

    system("pause");
    return 0;
}

I'm running into an issue where my function will not do anything. It will compile, but there is no output. The thing is when I take out the vector sPay from all the parts it works perfectly fine. Any suggestions as to why that one part isn't working? From my limited knowledge it should work perfectly fine, however I can't figure out what would be causing this.

My example text file is

Alan
WakeField
IT GUY
T2034
40
15
1
Hourly
0.00
aaro grill
  • 19
  • 4
  • I suggest you read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Some programmer dude Nov 09 '17 at 01:28
  • I also suggest you [pick up a couple of good beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn about *structures* and *classes* to group closely related data. And of course about local variables! – Some programmer dude Nov 09 '17 at 01:29
  • Ask yourself this: If not reading `sPay` allows the program to work, there must be something wrong with the input for it. What do you read in before it, and what's the next bit of data in your input file? – 1201ProgramAlarm Nov 09 '17 at 01:31
  • 1
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – NathanOliver Nov 09 '17 at 01:36

2 Answers2

1

Your input file does not match your reading code. There are 9 values in the file that you have shown, but your code is attempting to read only 8 values.

When readIn() gets to this code:

iFile >> dTemp;
sPay.push_back(dTemp);

It attempt to read a double but the file has Hourly instead, so the read fails.

So, either remove the Hourly line from the file, or else add a call to iFile >> sTemp to read that line.

Also, the parameters string sTemp, double dTemp, and int iTemp should be declared as local variables instead of as input parameters.

Also, readIn() is not doing any error handling. Your main() code makes an invalid assumption that the vector of first names exactly matches the size of the other vectors, but readIn() does not guarantee that.

And lastly, checking eof() before you have read anything is wrong. The stream's eofbit flag is not updated until a read operation attempts to read past EOF.

You should consider re-writing this code. For example, try something more like this:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

struct Employee
{
    std::string fName;
    std::string lName;
    std::string title;
    std::string eID;
    double hoursWorked;
    double wage;
    int deductions;
    std::string wageType;
    double sPay;

    Employee() :
        hoursWorked(0), wage(0), deductions(0), sPay(0)
    {
    }
};

void check(std::ifstream &iFile)
{
    if (!iFile.is_open())
    {
        std::cout << "Data file not found or unable to open!" << std::endl;
        std::system("pause");
        exit(1); // exit the program.
    }
}

void readIn(std::ifstream &iFile, std::vector<Employee> &employees)
{
    std::ios_base::iostate oldstate = iFile.exceptions();
    iFile.exceptions(std::ifstream::badbit | std::ifstream::failbit);

    try
    {
        do
        {
            Employee emp;

            iFile >> emp.fName;
            iFile >> emp.lName;
            std::getline(iFile, emp.title);
            iFile >> emp.eID;
            iFile >> emp.hoursWorked;
            iFile >> emp.wage;
            iFile >> emp.deductions;
            iFile >> emp.wageType;
            iFile >> emp.sPay;

            employees.push_back(emp);
        }
        while (!iFile.eof());
    }
    catch (const std::ios_base::failure &)
    {
        std::cout << "Data file corrupted!" << std::endl;
        std::system("pause");
        exit(1); // exit the program.
    }

    iFile.exceptions(oldstate);

    std::cout << "completed" << std::endl;
}

int main()
{
    std::ifstream iFile("data.txt");
    check(iFile);

    std::vector<Employee> employees;

    readIn(iFile, employees);

    int sizeOf = employees.size();
    for (int a = 0; a < sizeOf; a++)
    {
        std::cout << "FName " << employees[a].fName
                  << " LName " << employees[a].lName
                  << " JobTitle " << employees[a].title
                  << std::endl;
        std::cout << "EmployeeID " << employees[a].eID
                  << " Hours Worked " << employees[a].hoursWorked
                  << " << employees[a].wageType << " Wage " << employees[a].wage
                  << std::endl;
        std::cout << "Deductions " << employees[a].deductions
                  << " Salary Pay " << employees[a].sPay
                  << std::endl;
        std::cout << std::endl;
    }

    std::system("pause");
    return 0;
}

Alternatively, since your data is line-based, you should use std::getline() to read each line, and then use std::istringstream to parse values:

void readIn(std::ifstream &iFile, std::vector<Employee> &employees)
{
    std::string sTemp;

    std::ios_base::iostate oldstate = iFile.exceptions();
    iFile.exceptions(std::ifstream::badbit | std::ifstream::failbit);

    try
    {
        do
        {
            Employee emp;

            std::getline(iFile, emp.fName);
            std::getline(iFile, emp.lName);
            std::getline(iFile, emp.title);
            std::getline(iFile, emp.eID);

            std::getline(iFile, sTemp);
            if (!(std::istringstream(sTemp) >> emp.hoursWorked))
                iFile.setstate(std::ifstream::failbit);

            std::getline(iFile, sTemp);
            if (!(std::istringstream(sTemp) >> emp.wage))
                iFile.setstate(std::ifstream::failbit);

            std::getline(iFile, sTemp);
            if (!(std::istringstream(sTemp) >> emp.deductions))
                iFile.setstate(std::ifstream::failbit);

            std::getline(iFile, emp.wageType);

            std::getline(iFile, sTemp);
            if (!(std::istringstream(sTemp) >> emp.sPay))
                iFile.setstate(std::ifstream::failbit);

            employees.push_back(emp);
        }
        while (!iFile.eof());
    }
    catch (const std::ios_base::failure &)
    {
        std::cout << "Data file corrupted!" << std::endl;
        std::system("pause");
        exit(1); // exit the program if the file is corrupted.
    }

    iFile.exceptions(oldstate);

    std::cout << "completed" << std::endl;
}
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
0

I went derp for a moment. I forgot to read in the hourly or salaried before going to salary pay.

aaro grill
  • 19
  • 4