c++ HW Program that creates a linked list

Prompt:
Problem 3: Linked List (35 Points)

Using the Linked List program we did in class (or video) as an example write a program that creates another linked list using this file of Employee Data.

Here is a link to a partially completed program.
(You may start from scratch with your own code if you prefer.)

1. The structure for this example is shown below. Add a link where the comment indicates.

(Links to an external site.)
struct employee_data
{
string firstname;
string lastname;
char initial;
unsigned int ID;
double hours;
double wage;
double pay;
// Insert link here
};

int main(void)

{

2. Open the employee data file. Make sure to read and ignore the first two lines of the file.

3. Declare variables for the linked list: *root, *new_node, *current_node.

4. Create a linked list with ten nodes containing the data from the file.

5. Add a loop (or call a function) that will traverse the linked list printing out the data for all ten employees.

6. Find the data for a specific employee given the firstname and lastname.

Prompt the user to enter the firstname and lastname
Traverse the linked list to locate the node corresponding to the employee
Print out the node number and the full data for the employee.
8. TEST CASE: Diane Styles
My 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
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
//
// Create a linked list using data from the Employee_Data.txt file
//

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

struct employee_data
{
	string firstname;
	string lastname;
	char initial;
	unsigned int ID;
	double hours;
	double wage;
	double pay;
	// Insert link here
    employee_data *next;
}*head=NULL;


void print_data(employee_data person);
void enter_data(employee_data &person);
int main(void)
{
	ifstream infile;
	infile.open("/Users/adam/desktop/temp/emplyeedata1.txt");
	// You will need to change the path to the data file for your computer
	string line;
	employee_data *employee = new employee_data; // Pointer to employee_data type

	if (!infile)
	{
		cout << "Unable to open the employeed data file." << endl;
		return 1; // Error code for file open error
	}

	cout << "Employee Data File opened." << endl;
	// Read and ingnore the first two lines of the file.
	// They are not data lines.

	getline(infile, line);
	getline(infile, line);

	// Read in the data for the first employee
	infile >> employee->firstname >> employee->lastname >> employee->initial
		   >> employee->ID >> employee->hours >> employee->wage
		   >> employee->pay;
    enter_data(*employee);
	print_data(*employee);

	// 1. Add code to create a root node for the linked list
	// You will need to devlare pointers for *root, *current_node and *new_node
	// Fill in the data from the first employee.
	// You can use the data already input above for the first employee
	// if you want to.
    head=employee;
    employee->next=NULL;


	// 2. Now write a loop that will read and link the remaining nine
	// employee data records into a linked list.
    for(int i = 0; i < 9; i++)
    {
        employee_data *new_node = new employee_data;
        employee_data *cur=head;

        while (cur->next)
        {
            cur=cur->next=NULL;
        }

         infile >> new_node->firstname >> new_node->lastname >> new_node->initial            
                >> new_node->ID >> new_node->hours >> new_node->wage
                >> new_node->pay;

    cur->next=new_node;
    new_node->next=NULL;
    }

	// 3. Traverse the linked list printing out the data for each employee
	// You can call the print_data function in a loop for this.
    employee_data *cur=head;
    while(cur)
    {
        print_data(*cur);
        cur=cur->next;
    }

	return 0;
}
void enter_data(employee_data &person)
{
    cout << "Enter first and last name of employee: " << endl;
    cin >> person.firstname >> person.lastname;
    cout << endl;
}
void print_data(employee_data person)
{
	cout << endl;
	cout << fixed << setprecision(2) << left;
	cout << person.firstname << " " << person.initial
		 << " " << person.lastname << endl;
	cout << setw(15) << "ID:" << person.ID << endl;
	cout << setw(15) << "Hours Worked:" << person.hours << endl;
	cout << setw(15) << "Wage:" << person.wage << endl;
	cout << setw(15) << "Gross Pay:" << person.pay << endl << endl;
	cout << right;
	return;
}

My output:

Employee Data File opened.
Enter first and last name of employee: 
Louis Nelson


Louis R Nelson
ID:            3467
Hours Worked:  40.00
Wage:          17.75
Gross Pay:     710.00

zsh: segmentation fault  "/Users/adam/C++classimportant/"HW5problem3
(base) adam@Adams-MBP C++classimportant % cd "/Users/adam/C++classimportant/" && g++ HW5problem3.cpp -o HW5problem3 && "/Users/adam/C++classimportant/"HW5problem3
Employee Data File opened.
Enter first and last name of employee: 
Carter Wilkes


Carter R Wilkes
ID:            3467
Hours Worked:  40.00
Wage:          17.75
Gross Pay:     710.00

It prints put the names from the text file when I enter it, however it does not update the wage, hours and gross pay for some weird reason. There is also a segmentation fault error and I'm unfamiliar with that error.
The data file is below:

EMPLOYEE RECORDS for ACME CORPORATION
FIRST NAME     LAST NAME     INITIAL     ID NUMBER     HOURS WORKED     WAGE     GROSS PAY
Louis          Nelson        R           3467          40               17.75    710.00
Carter         Wilkes        T           6534          42               20.25    850.50
Mary           Connor        C           2390          40               22.50    900.00
Jennifer       Patton        N           4231          35               18.90    661.50
Jimmy          Masterson     T           5719          40               25.75    1030.00
Diane          Styles        M           6091          38               21.75    826.50
Kevin          Hodges        E           6723          30               15.00    450.00
Carrol         Britton       P           2845          40               20.25    810.00
Emily          Craft         A           7120          40               23.50    940.00
Ramona         Johnson       S           6419          40               27.50    1100.00
Last edited on
1
2
3
4
5
6
        employee_data *cur=head;

        while (cur->next)
        {
            cur=cur->next=NULL;
        }


What's that supposed to do?
Lines 52 and 53. You overwrite the first & last name of the first record, then print the result:
$ ./foo
Employee Data File opened.
Enter first and last name of employee:
Scooby Doo


Scooby R Doo
ID:            3467
Hours Worked:  40.00
Wage:          17.75
Gross Pay:     710.00

Segmentation fault (core dumped)


The assignment contains a list. I would add each item from the list to your code (as a comment. That way you know that you've done everything required and the prof can see it too.

For example, if you do this, you'll see that the assignment asks for variables root, new_node, and current. You have head, new_node, employee, and cur.

It's expensive to add to the end of a linked list. A common way around this is to keep a pointer to the last node. This is usually called a tail pointer.

Here is code that contains the assignment as comments and reads the file. You'll need to add the rest.
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
//
// Create a linked list using data from the Employee_Data.txt file
//

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

// 1. The structure for this example is shown below. Add a link where the
//    comment indicates.

struct employee_data
{
    string firstname;
    string lastname;
    char initial;
    unsigned int ID;
    double hours;
    double wage;
    double pay;
    // Insert link here
    employee_data *next;
};

void print_data(employee_data person);
void enter_data(employee_data & person);
int
main(void)
{
    string line;
    
    ifstream infile;
    // You will need to change the path to the data file for your computer

    // 2. Open the employee data file. Make sure to read and ignore the first
    //    two lines of the file.
    infile.open("/Users/adam/desktop/temp/emplyeedata1.txt");
    if (!infile) {
	cout << "Unable to open the employeed data file." << endl;
	return 1;				 // Error code for file open error
    }

    cout << "Employee Data File opened." << endl;

    getline(infile, line);
    getline(infile, line);

    // 3. Declare variables for the linked list: *root, *new_node,
    // *current_node.
    employee_data *root = NULL, *new_node, *current = NULL;

    // 4. Create a linked list with ten nodes containing the data
    // from the file.
    while (true) {
	new_node = new employee_data;
	if (!(infile >> new_node->firstname >> new_node->lastname >> new_node->initial
	      >> new_node->ID >> new_node->hours >> new_node->wage >> new_node->pay))
	    break;
	if (current) {
	    current->next = new_node;
	} else {
	    root = new_node;
	}
	current = new_node;
    }
    if (current) current->next = NULL;	// terminate the list
    
    // 5. Add a loop (or call a function) that will traverse the
    //    linked list printing out the data for all ten employees.

    // 6. Find the data for a specific employee given the firstname
    // and lastname.

    //    Prompt the user to enter the firstname and lastname

    //    Traverse the linked list to locate the node corresponding
    //    to the employee

    //    Print out the node number and the full data for the employee.

    // 8. TEST CASE: Diane Styles
    return 0;
}

void
enter_data(employee_data & person)
{
    cout << "Enter first and last name of employee: " << endl;
    cin >> person.firstname >> person.lastname;
    cout << endl;
}

void
print_data(employee_data person)
{
    cout << endl;
    cout << fixed << setprecision(2) << left;
    cout << person.firstname << " " << person.initial << " " << person.lastname << endl;
    cout << setw(15) << "ID:" << person.ID << endl;
    cout << setw(15) << "Hours Worked:" << person.hours << endl;
    cout << setw(15) << "Wage:" << person.wage << endl;
    cout << setw(15) << "Gross Pay:" << person.pay << endl << endl;
    cout << right;
    return;
}


Topic archived. No new replies allowed.