getline from file produces minor formatting error

closed account (ybSNAqkS)
I'm doing a basic assignment that involves reading strings and numerical values from a text file, and the resulting output must be aligned a certain way. Here's a small example of the file data being input to the program:

1
2
3
John Smith#40
Jane Doe#45
Harry Morgan#40 

In Notepad, the new line characters don't appear, and it looks like a line of text, but there are clearly new line characters here between the integers and the next string.

So my code to read the input of the file looks like so. Please assume the file has been properly opened and the data is successfully input as shown, because it is.
1
2
3
        getline(fileInput,employeeName,'#');
        fileInput>>hoursWorked;
        cout<< setw(22) << name << setw(10) << hours;

The widths 22 and 10 are the same widths as the header before it, and the first line of data is output correctly, in line with the header. The problem is with EVERY line afterward... Here is an example of what the result looks like
1
2
3
4
Name           Hours
Person A       40.00
Person B      45.00
Person C      45.00


Every line after seems to align incorrectly even though the cout instruction is identical... so, why exactly is this???
Last edited on
You haven't posted enough code for anyone to make sense of this problem.

You could help yourself by using setfill() in the stream to sed the fill character to something other than space so you can see formatted the fields.
closed account (ybSNAqkS)
My apologies, I tried to avoid being tl;dr. Here is the code in full, omitting headers and prototypes.

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
int main()
{
  	// 1) connect the input file object to payroll.txt
  	ifstream fileInput;
  	fileInput.open("hours.txt");

  	// 2) initialize accumulaton variables
  	string employeeName;
  	double hoursWorked=0.00;

  	reportTitle();

    // 5) using an eof() controlled repetition structure, read the lines
    //    of the input file, display the formatted lines and extra data
    //    update any totals
    while (fileInput.eof()==false)
    {
        // Get the employee's named
        getline(fileInput,employeeName,'#');
        fileInput>>hoursWorked;
        displayEmployeeInfo(employeeName,hoursWorked);
    }

	// 7) disconnect from the input file
	fileInput.close();
}

// reportTitle() displays the payroll report titles in columnar format
void reportTitle()
{
	// set program formatting
	cout << setprecision(2) << fixed << showpoint << left;

	// display report titles
	cout << setw(22) << "Employee" << setw(10) << "Hours" << endl;
}

// displayEmployeeInfo(name, hourly, hours, taxrate, gross, net)
// displays payroll information for an employee
void displayEmployeeInfo(string name, double hours)
{
    cout << setw(22) << name << setw(10) << hours;
}


Again, my output for every line produced by the while loop looks like this:
1
2
3
4
Name           Hours
Person A       40.00
Person B      45.00   // Every line here onward is formatted slightly wrong by one space
Person C      45.00


I suspect it has something to do the input stream but I've no idea where to begin tackling this.
Last edited on
1
2
3
4
5
6
while( std::getline( fileInput, employeeName, '#' ) && fileInput >> hoursWorked )
{
    std::cout << std::setw(22) << employeeName << std::setw(10) << hoursWorked << '\n' ;
    fileInput.ignore( 1000, '\n' ) ; // extract and discard the new line after hoursWorked
                                     // (or else it will be a part of the next name that is read)
}

http://coliru.stacked-crooked.com/a/834772e707c5593b
closed account (ybSNAqkS)
Works like a dream. I did some further reading into ifstream::ignore. Quite handy for use with the input stream. Cheers.
Last edited on
Topic archived. No new replies allowed.