0
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    ifstream PayrollDoc;
    PayrollDoc.open("hours.txt");
    string line;
    int counter = 0;
    string Name, WorkerID;
    float Hours, Wage, Paycheck;
        if (PayrollDoc.is_open())
        {
            while (!PayrollDoc.eof())
            {
                if (counter == 0)
                {
                    getline(PayrollDoc, Name);
                    counter++;
                }
                else if (counter == 1)
                {
                    getline(PayrollDoc, WorkerID);
                    counter++;
                }
                else if (counter == 2)
                {
                    std::istringstream PayrollDoc(line);
                    cin >> Hours;
                    counter++;
                }
                else if (counter == 3)
                {
                    std::istringstream PayrollDoc(line);
                    cin >> Wage;
                    counter++;
                }
                else if (counter == 4)
                {
                    Paycheck = Hours * Wage;
                    cout << Name << WorkerID << Paycheck << endl;
                    counter = 0;
                }
            }

        }
        else
        {
            cout << "The specified file could not be found.";
        }
        return 0;
}

And the text file looks like this:

A00115
John Doe
36.5
25.0
A01256
Jane Doe
40.0
28.0

My compiler is not giving me any errors, but gives no output and I am quite truly stumped. It should output the Name, ID number, and paycheck amount on one one line. I am confused as how to use getline for floats, any help would be very appreciated.

  • `while (!PayrollDoc.eof())` is a common error covered in detail by [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Sep 24 '19 at 02:15
  • Run the program in the debugger that came with your development environment (and STRONGLY consider getting a different development environment if it doesn't have a debugger) and step through the program line by line to see what it's really doing. Very enlightening. – user4581301 Sep 24 '19 at 02:19

1 Answers1

0

Well, if you want it in one line don't put "endl" that makes it skip a line, also use a switch like

Switch(counter)
{
case 1:
    //Code
    Counter++;

    Break

}

how would a piece would be accessed if the code needs x to run, but to run x - you needed y and it can get confusing, don't use a bunch of if 's

whoami - fakeFaceTrueSoul
  • 13,846
  • 4
  • 23
  • 39
Arath
  • 1
  • 2