ifstream going into fail state

I'm writing a program for an assignment. It is a stationary kiosk where the user can select designs of stationaries and when they are done ordering it prints an invoice to the screen. The problem I'm having is that the program runs fine the first time but on the second iteration of the loop, when it hits the getline() function it sends ifstream into the failstate and the style variable is "NULL". The sheetsOrdered and envelopesOrdered variables then hold the same variables. Why is ifstream going into the fail state?

Here is the 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
void printInvoice (string header, int stationariesOrdered)
{
	ifstream orderInput;
	string style;
	int sheetsOrdered = 0;
	int envelopesOrdered = 0;
	int iteration = 0;
	double pricePerSheet = .2;
	double pricePerEnvelope = .25;
	double styleTotal[99];
	double subtotal = 0.00;
	double tax = 0.00;
	double total = 0.00;

	clrscr();
	timeHeader();
	cout << endl << endl;
	cout << "\t\t\tJorge and Marissa's Stationary Shop" << endl << endl;
	cout << header << endl << endl;
	cout << "Style\t\tNo. Pgs.\tPrice/\t   No. Env.\tPrice/   \tTotal" << endl;
	cout << "\t\t\t\t Pg.\t\t\t Env." << endl;
	cout << "--------------------------------------------------------------------------------" << endl;

	orderInput.open("orderinformation.dat");

	do
	{
		getline (orderInput, style);
		orderInput >> sheetsOrdered;
		orderInput >> envelopesOrdered;
		styleTotal[iteration] = (sheetsOrdered * pricePerSheet) + (envelopesOrdered * pricePerEnvelope);
		cout << style << '\t' << sheetsOrdered << '\t' << pricePerSheet << '\t' << envelopesOrdered << '\t'
			 << pricePerEnvelope << '\t' << styleTotal[iteration];
		stationariesOrdered--;
		iteration++;
	}while (stationariesOrdered >= 0);

	for (int count = 0; count < iteration; count++)
	{
		subtotal = subtotal + styleTotal[count];
	}

}


An example of the contents of orderinformation.dat would be:

style name
3
3
style name 2
3
3
style name 3
3
3

etc.
Here is how it is looking at your file:

style name\n
3\n
3\n
style name2\n
3\n
3\n
...

After the first getline(), it takes the entire line and also removes the \n for you.
Next, it reads the 3, but does not discard the \n. On the next read, >> discards the leading \n then reads the 3, again not discarding the trailing \n. After that, on your next loop, getline() immediately sees a \n and sees that as the entire like, returning "" and discarding it. Therefore on the next read of an int using >>, it sees "style name2" which is not valid for a int, which puts it into an error state.
Thank you very much. I figured it had to be something simple I was just missing. I appreciate the help.
Topic archived. No new replies allowed.