Linked List

I am struggling with how to build a linked list. My program is a menu driven program with 6 options. The first option is to add a file. The user is prompted to enter name, id number, hours worked, and pay rate. This data is then written to a text file. All of that is good. However the 2nd option to the menu is to display a record which is where im lost. I the program is supposed to read the data from the text file into a linked list. From there the user is prompted to type in a last name and then it will search for a record based on that last name. This is where im stuck. I got the text file written, but how do I write that text file to a linked list? I think I understand how to search, but taking the data from the text and putting it into a linked list is where im lost. Any help would be appreciated. I posted my code for the text file below

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
void Menu(int MenuSelection, bool valid, string &EmployeeName, string &FirstName, string &LastName, int &EmployeeNumber, double &Hours, double &PayRate, char &UnionCode){
	cout << "Payroll Maintenance Program" << endl;
	cout << "         Main Menu" << endl;
	cout << endl;
	cout << "     1. Add a Record" << endl;
	cout << "     2. Display a Record" << endl;
	cout << "     3. Display the File" << endl;
	cout << "     4. Modify a Record" << endl;
	cout << "     5. Delete a Record" << endl;
	cout << "     6. Exit Program" << endl;
	cout << endl;
	cout << "  Enter your selection (1-6)-> ";
	cin >> MenuSelection;
	cout << endl;

	while (!valid){//verifies only menu numbers 1-6 are typed in
		if (MenuSelection < 7 && MenuSelection >0){
			valid = true;
		}
		else {
			cout << "Invalid Input, Please Enter a Number 1-6 Only ";
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			cin >> MenuSelection;
			cout << endl;
		}
	}

	if (MenuSelection == 1){
		GetInput(EmployeeName, FirstName, LastName, EmployeeNumber, Hours, PayRate, UnionCode);
		Menu(MenuSelection, valid, EmployeeName, FirstName, LastName, EmployeeNumber, Hours, PayRate, UnionCode);
	}

	if (MenuSelection == 2){
		cout << "Display Record" << endl;
	}
	if (MenuSelection == 3){
		cout << "Display File" << endl;
	}
	if (MenuSelection == 4){
		cout << "Modify" << endl;
	}
	if (MenuSelection == 5){
		cout << "Delete" << endl;
	}
	if (MenuSelection == 6){
		cout << "Exit" << endl;
	}
}

void GetInput(string &EmployeeName, string &FirstName, string &LastName, int &EmployeeNumber, double &Hours, double &PayRate, char &UnionCode){
	
	
	cout << "Enter Employee's First and Last Name: ";
	cin >> FirstName;
	cin >> LastName;
	cout << endl;
	EmployeeName = LastName + ", " + FirstName;
	cout << "3 Digit Employee #: ";
	cin >> EmployeeNumber;
	cout << endl;
	cout << "Hours Worked: ";
	cin >> Hours;

	while (Hours > 60){  //Hour verification, must be between 0.0 - 60.0 
		cout << endl;
		cout << "Hours must be between 0.0 and 60.0" << endl;
		cout << "Please Enter Hours Again: ";
		cin >> Hours;
		cout << endl;
	}

	cout << "Pay Rate: $";
	cin >> PayRate;

	while (PayRate < 7.50 || PayRate > 45.00){  //Pay Rate verification, must be between $7.50 and $45.00
		cout << endl;
		cout << "Pay rate must be between $7.50 and $45.00" << endl;
		cout << "Please Enter Pay Rate Again: $";
		cin >> PayRate;
		cout << endl;
	}

	cout << "Union Code(A,B,C): ";
	cin >> UnionCode;

	while (UnionCode != 'A' && UnionCode != 'B' && UnionCode != 'C'){ //Union Code verification, must be A, B, or C
		cout << endl;
		cout << "Union Code can only be A, B, or C" << endl;
		cout << "Please Enter Union Code Again: ";
		cin >> UnionCode;
		cout << endl;
	}
	cout << endl;

	ofstream myfile("Employee_Data.txt", fstream::app);
	myfile << "Name: " << EmployeeName << endl;
	myfile << "Employee Number: " << EmployeeNumber << endl;
	myfile << "Union Code: " << UnionCode << endl;
	myfile.precision(2);
	myfile.setf(ios::fixed);
	myfile.setf(ios::showpoint);
	myfile << "Hours: " << Hours << endl;
	myfile << "Rate: $" << PayRate << endl;
	myfile.close();

	cout << "Record Has Been Added" << endl;
	cout << endl;
}
It sounds like you need to create a struct in your linked list to hold your data.
http://www.bogotobogo.com/cplusplus/linkedlist.php#linkedlistexample4

After you implement the above list for your data struct, then you would need to modify your code to accept your pre-formatted input from the data in your text file.

See the links below for other examples .

http://www.cprogramming.com/tutorial/lesson15.html
http://www.bogotobogo.com/cplusplus/linkedlist.php
Topic archived. No new replies allowed.