Getline not reading the first character, despite there being no ignore statement

I have to write a program which reads a file that is this list:

Bread
2.49
Y
Milk
1.89
N
Eggs, dozen
0.97
N
Apples
4.75
Y
Bananas
1.69
Y
Peanut Butter
2.49
Y

It has to print a receipt and be able to work if items are added to the list. When I run my program it looks perfect, except it is not reading the first character of the item names.

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
#include "stdafx.h"
#include "stdio.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;


int main()
{
	ifstream inFile;
	string name;
	char taxable;
	double	price = 0,
		subtotal = 0,
		tax = 0,
		total = 0;
	inFile.open("HW3_Data.txt");

	cout << "Thank you for shopping at StuffMart" << endl;
	cout << left << setw(21) << "Item Name" << "Unit Price Tax" << endl;
	cout << setw(35) << setfill('_') << "" << endl << setfill(' ');

	while	(inFile >> taxable) {

			std::getline(inFile, name, '\n');

			inFile >> price >> taxable;
			subtotal = subtotal + price;

			tax = (taxable == 'Y' ? tax + (.085 * price) : tax);

			cout	<< left << setw(21) << name
					<< "$" << setw(9) << right << price << "  " << taxable << endl;
			}

	cout << setw(20) << "" << setw(15) << setfill('_') << "" << endl << setfill(' ');
	total = subtotal + tax;
	cout	<< right << setw(20) << "Subtotal   " << "$" << subtotal << endl
			<< setw(20) << "Tax (8.5%)   " << "$" << tax << endl
			<< setw(20) << "Total   " << "$" << total << endl;

	inFile.close();

	system("pause");
	return 0;
}
Last edited on
What I've tried so far:

Using getline on the first item separately, before the while loop. This only fixed the problem for the first item.

Using different conditions for the while loop such as:
while (inFile >> name >> price >> taxable)
while (inFIle >> name)
while (inFIle >> price)
while (!inFile.eof())

Using various ignore statements in different positions within the while loop. These only added to the problem because then it would ignore even more characters in the name.
Change lines 25-29 to
1
2
        while   ( getline( inFile, name ) ) {
                        inFile >> price >> taxable;   inFile.ignore( 1000, '\n' );
Or...

1
2
3
     while (getline(inFile, name) >> price >> taxable >> std::ws) {
        // ...
    }


http://en.cppreference.com/w/cpp/io/manip/ws
Thank you very much for the help!
Topic archived. No new replies allowed.