I need help fast

Im writing a program that reads from a data file, does some math, and spits out some stuff to the console. It keeps reading the last line of the data file twice for some reason.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

const float IN_PART_TIME = 50.00;
const float IN_FULL_TIME = 700.00;
const float OUT_PART_TIME = 100.00;
const float OUT_FULL_TIME = 1400.00;
const float DISCOUNT = .025;

ifstream inData;
string studentID;
float hoursEnrolled;
char residentialCode;
string residentialStatus;
char discountCode;
float tuitionDue;
int studentsEnrolled = 0;
float total = 0.00;

int main()
{
	inData.open("Tuition.txt");

	cout << "Tim-Buck-Two School of Hard Knocks" << endl
		<< "Tuition Report for Today" << endl
		<< "Created by Name" << endl << endl;

	cout << "Student   Hours      Residential   Discount   Tuition" << endl
		<< "  ID      Enrolled   Status        Amount     Due" << endl;

	if (inData)
	{
		while (!inData.eof())
		{
			inData >> studentID >> hoursEnrolled >> residentialCode >> discountCode;

			float discountAmount = 0.00;

			//	Determine if full/part-time and in/out
			if (hoursEnrolled < 12)
			{
				if (residentialCode == 'I')
				{
					residentialStatus = "IN";
					tuitionDue = IN_PART_TIME;
				}
				else if (residentialCode == 'O')
				{
					residentialStatus = "OUT";
					tuitionDue = OUT_PART_TIME;
				}
			}
			else
			{
				if (residentialCode == 'I')
				{
					residentialStatus = "IN";
					tuitionDue = IN_FULL_TIME;
				}
				else if (residentialCode == 'O')
				{
					residentialStatus = "OUT";
					tuitionDue = OUT_FULL_TIME;
				}
			}

			//	Determine if discounted
			if (discountCode == 'Y')
			{
				discountAmount = tuitionDue * DISCOUNT;
				tuitionDue -= discountAmount;
			}

			total += tuitionDue;

			studentsEnrolled++;

			cout << setw(6) << studentID << "    " << setw(4) << fixed << setprecision(1) << hoursEnrolled
				<< "       " << left << setw(3) << residentialStatus << "           $" << setw(5) << fixed << setprecision(2) << discountAmount
				<< "     $" << fixed << setprecision(2) << tuitionDue << endl;
		}
		cout << endl << "Students Enrolled:   " << studentsEnrolled << endl
			<< "Total Tuition:       $" << fixed << setprecision(2) << total << endl;
	}
	else
	{
		cout << "FILE NOT FOUND" << endl;
	}
	system("pause");
	return 0;
}


And here is what is in the data file:

181239 13 I Y
091243 15.5 O N
872381 14 I N
992398 13.5 O Y
981237 10.5 I Y
987123 9 O N
738829 4.5 I N
873294 11.5 O Y
098372 17.5 I N
387923 16 O N
982347 5 O Y
473829 14 I N
983872 13 I N
908234 17 O Y
764828 19 O N
Last edited on
You should read the entire line with std::getline, then parse that line using istringstream.

You're not reading the last line twice, it's just that you haven't hit EOF until you attempt to read the next studentID. The read fails, but the variables all have the input from the last line, so you think it's read them again, it hasn't read anything.

Perhaps a much simpler way is not do it like this:
1
2
3
4
5
std::ifstream inData.open("Tuition.txt");
while (inData >> studentID >> hoursEnrolled >> residentialCode >> discountCode)
{
    // process values
}
Last edited on
I did what you said and it worked but I don't really understand why. Thank you though!
Topic archived. No new replies allowed.