Only second line of File Input will print

I am inputting 2 simple data files into a linked list.
FILE 1{
17 5.25 Adam Parker
18 6.75 John F. Jones
}
The first column represents Employee ID number
The second column represents hourly wage
The third column represents Employee name

FILE 2{
17 40
18 20
18 20
}
The first column represents Employee ID number
The second column represents number of hours worked

The output should be:
*********Payroll Information********
John F. Jones, $270
Adam Parker, $210
*********End payroll**************

However, my output is: (i.e. only shows first line)
*********Payroll Information********
John F. Jones, $270
*********End payroll**************

Does anyone know how to formulate a proper ranged for loop?

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

void openInputFile(ifstream& inFile) {
	string filename;
	cout << "What filename? ";
	cin >> filename;
	inFile.open(filename);
	while (!inFile) {
		cout << "File failed to open!" << endl;
		cout << "What filename? ";
		cin >> filename;
		inFile.clear();
		inFile.open(filename);
	}
}
template <class T>
class LList; //just a pre-definition

template <class T>
class LListNode {
public:
	T data;
	LListNode<T>* next;
	LListNode(T newdata=T(), LListNode* newNext = nullptr)
		:data(newdata), next(newNext) {}
	friend class LList<T>; 
};

template <class T>
class LList {
	LListNode<T>* head;
	int numOfElements;
	LListNode<T>* recursiveCopy(LListNode<T>* ptr);
public:
	LList() { head = nullptr; numOfElements = 0; }
	LList(const LList<T>& rhs) : head(nullptr), numOfElements(0) { *this = rhs; }
	LList<T>& operator=(const LList<T>& rhs);
	~LList() { clear(); }
	bool isEmpty()const { return head == nullptr; }

	void insertAtHead(T newItem);
	T removeFromHead();
	int size()const;
	void clear();
	void insertAtTail(T newItem);
	void insertAfter(LListNode<T>* location, T newitem);
	T removeFromTail();
};
template <class T>
LListNode<T>* LList<T>::recursiveCopy(LListNode<T>* ptr) {
	if (ptr == nullptr)
		return nullptr;
	return new LListNode<T>(ptr->data, recursiveCopy(ptr->next));
}

template <class T>
LList<T>& LList<T>::operator=(const LList<T>& rhs) {
	if (this == &rhs)
		return *this;
	clear();
	head = recursiveCopy(rhs.head);  
	return *this;
}

template <class T>
T LList<T>::removeFromTail() {
	if (isEmpty())//test for stupid user
		return 0;
	else if (head->next == nullptr) //only one node
		return removeFromHead();
	LListNode* temp = head;
	for (; temp->next->next != nullptr; temp = temp->next);						

															//delete the node AFTER temp
	LListNode<T>* toDelete = temp->next;
	temp->next = nullptr;
	T toReturn = toDelete->data;
	delete toDelete;
	numOfElements--;
	return toReturn;
}
template <class T>
void LList<T>::insertAtTail(T newItem) {
	if (isEmpty())
		insertAtHead(newItem);
	else {
		LListNode<T>* temp = head;
		for (; temp->next != nullptr; temp = temp->next); //find the last node!
														 
		insertAfter(temp, newItem); //insertAfter the last node
	}
}
template <class T>
void LList<T>::insertAfter(LListNode<T>* location, T newItem) {
	location->next = new LListNode<T>(newItem, location->next);
	numOfElements++;
}
template <class T>
void LList<T>::clear() {
	while (!isEmpty())
		removeFromHead();
}
template <class T>
int LList<T>::size()const {
	return numOfElements; //Constant time

						
template <class T>
T LList<T>::removeFromHead() {
	if (isEmpty()) //Test for dumb user!
		return T();
	LListNode<T>* toDelete = head; 
	head = head->next; 

	T toReturn = toDelete->data;
	delete toDelete;
	numOfElements--;
	return toReturn;
}
template <class T>
void LList<T>::insertAtHead(T newItem) {
	head = new LListNode<T>(newItem, head);
	numOfElements++;
}

class Employee {
public:
	double wage(double hourlyRate, int hours_worked);
	/*
	Employee(int ID, double rate, string EmployeeName);
	Employee(int ID, int hours_worked);
	Employee();
	int getIDnum() const;
	int getnum_hours_worked()const;
	double gethourlyRate() const;
	string getname() const;
	void setIDnum(int ID);
	void setnum_hours_worked(int hours_worked);
	void sethourlyRate(double rate);
	void setname(string EmployeeName);
	*/
	string name;
	double hourlyRate;
	int IDnum, num_hours_worked;
};
double Employee::wage(double hourlyRate, int hours_worked) {
	return (hourlyRate*double(hours_worked));
}
/*
Employee::Employee(int ID, double rate, string EmployeeName) {
	setIDnum(ID);
	sethourlyRate(rate);
	setname(EmployeeName);
}
Employee::Employee(int ID, int hours_worked) {
	setIDnum(ID);
	setnum_hours_worked(hours_worked);
}
Employee::Employee(){
	setIDnum(0);
	setnum_hours_worked(0);
	sethourlyRate(0);
	setname(" ");
}
int Employee::getIDnum() const {
	return IDnum;
}
int Employee::getnum_hours_worked() const {
	return num_hours_worked;
}
double Employee::gethourlyRate() const {
	return hourlyRate;
}
string Employee::getname() const {
	return name;
}
void Employee::setIDnum(int ID) {
	IDnum = ID;
}
void Employee::setnum_hours_worked(int hours_worked) {
	num_hours_worked = hours_worked;
}
void Employee::sethourlyRate(double rate) {
	hourlyRate = rate;
}
void Employee::setname(string EmployeeName) {
	name = EmployeeName;
}
*/
int main() {
	ifstream inFile1;
	openInputFile(inFile1);

	LList<Employee> EmployeeList;
	Employee temp1;	

	while (inFile1 >> temp1.IDnum) {
		inFile1 >> temp1.hourlyRate;
		inFile1.ignore(1, ' ');
		getline(inFile1, temp1.name);
		EmployeeList.insertAtHead(temp1);
	}
		
	cout << temp1.IDnum << endl;
	cout << temp1.hourlyRate << endl;
	cout << temp1.name<< endl;

	ifstream inFile2;
	openInputFile(inFile2);
	
	LList<Employee> PayrollList;
	while (inFile2 >> temp1.IDnum>> temp1.num_hours_worked){
		if (temp1.IDnum == temp1.IDnum) {
			temp1.num_hours_worked += temp1.num_hours_worked;
		}
		PayrollList.insertAtHead(temp1);
	}

	
	cout << "*********Payroll Information********" << endl;
	cout << temp1.name << ", " << temp1.wage(temp1.hourlyRate, temp1.num_hours_worked) << endl;
	cout << "*********End payroll**************" << endl;
	

	return 0;
}
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/doc/tutorial/files/

Read this, there are samples there that show you what to do.
> Does anyone know how to formulate a proper ranged for loop?
Here (tutorial) :
http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html
Topic archived. No new replies allowed.