No capitals...

I'm running the following code:
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
44
45
46
47
48
49
50
51
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	ifstream inData;
	ofstream outData;

	inData.open("Payroll_input.txt");
	outData.open("Payroll_output.txt");

	string employeeName, errorName;
	int employeeID, hours, errorHours;
	double payRate, errorPayRate;
	bool trueFalse = false;
	char ch;

	outData << left << setw(20) << "Employee name" << setw(20) << "Hourly pay rate" << setw(15) << "Hours worked" << setw(10) << "Tax rate" << setw(10) << "Gross pay" << setw(10) << "Net pay" << endl;

	for (int i = 1; i <= 5; i++)
	{
		getline(inData, employeeName);
		inData >> employeeID >> hours >> payRate;
		if (inData.peek() == '\n')
			inData >> ch;
		if (hours < 0 || payRate < 6)
		{
			errorName = employeeName;
			errorHours = hours;
			errorPayRate = payRate;
			trueFalse = true;
			continue;
		}
	outData << setw(20) << employeeName << setw(20) << payRate << setw(15) << hours << setw(10) << .1 << setw(10) << .9 * payRate * hours << setw(10) << payRate * hours << endl;
	}

	if (trueFalse)
	{
		outData << "\nAn error has occurred while processing the following data\n";
		outData << setw(20) << "Employee name" << setw(20) << "Hourly pay rate" << setw(15) << "Hours worked" << setw(10) << "Tax rate" << setw(10) << "Gross pay" << setw(10) << "Net pay" << endl;
		outData << setw(20) << errorName << setw(20) << errorPayRate << setw(15) << errorHours << setw(10) << .1 << setw(10) << .9 * errorPayRate * errorHours << setw(10) << errorPayRate * errorHours << endl;
	}
	inData.close();
	outData.close();

	return 0;
}

With the following input:
Joe John
10001 45 25.00
Random Guy
10002 35 20.00
Incorrect Input
10003 -10 5.00
An Employee
10004 10 15.00
One More
10005 25 25.00


And I keep getting the following output:
Employee name Hourly pay rate Hours worked Tax rate Gross pay Net pay
Joe John 25 45 0.1 1012.5 1125
andom Guy 20 35 0.1 630 700
n Employee 15 10 0.1 135 150
ne More 25 25 0.1 562.5 625

An error has occurred while processing the following data
Employee name Hourly pay rate Hours worked Tax rate Gross pay Net pay
ncorrect Input 5 -10 0.1 -45 -50

For some reason, the first letter of the names coming after Joe John are being omitted. Please help, I cannot figure out why this is happening. Thank you very much in advance!
Last edited on
1
2
  if (inData.peek() == '\n')
      inData >> ch;
is the problem.
because >> reads in characters and not the line feed it will read in the next char which is the first character of the next employees name hence the missing letter.

try this instead:
1
2
if ( inData.peek() == '\n')
	inData.ignore(1);
the ignore function will read and discard the WHATEVER the next character is (in this case the new line) so everything will now be correct.
So an important mental note would be that >> ignores '\n'... Right?... Thanks guestgulkan!
I wouldn't quite put it like that - I would say that it treats it like a seperator - the same way it treats the spaces between the values.
Topic archived. No new replies allowed.