user8577304
user8577304

Reputation:

Printing Out Linked List - C++

I'm trying to print out all of the employees in my linked list but am encountering an issue to where all of but the last employee is being printed out. I have a printRoster() function to where it prints out all of the names of my list correctly which is 3 total, but my print function only seems to print out just 2. (I can post more code if necessary)

Here is my text file:

START_OF_FILE

INSERT_EMPLOYEE
123456
John
Smith
64000
35

INSERT_EMPLOYEE
345678
Mike
Jones
70000
30

INSERT_EMPLOYEE
234567
Dean
Thomas
72000
40

PRINT_ROSTER

PRINT_EMPLOYEE
John
Smith

PRINT_EMPLOYEE
Mike
Jones

PRINT_EMPLOYEE
Dean
Thomas

END_OF_FILE

My output:

John Smith, 123456
Mike Jones, 345678
Dean Thomas, 234567

John Smith, 123456
Salary: 64000
Hours: 35

Mike Jones, 345678
Salary: 70000
Hours: 30

Expected output:

John Smith, 123456
Mike Jones, 345678
Dean Thomas, 234567

John Smith, 123456
Salary: 64000
Hours: 35

Mike Jones, 345678
Salary: 70000
Hours: 30

Dean Thomas, 234567
Salary: 72000
Hours: 40

Upvotes: 1

Views: 79

Answers (1)

Ajinkya Dhote
Ajinkya Dhote

Reputation: 1478

Problem is in your printEmployee function's while loop while (tempEmployee->next != NULL) You are checking if next employee is present or not, and if it is present then and only then your loop is executed.

In your case when your loop is at last employee, it checks if next employee is present or not and as it is not present your loop is not executed and the info of last employee is not printed.

you should change your while loop like this

while(tempEmployeee != NULL)

Upvotes: 1

Related Questions