In multiple compilers it prints only 7 lines from the file but there are MANY more lines but logically and syntax-wise the code looks good to me. Am I missing something? At first I thought it was the compiler but now I feel like there could be something wrong with my code.
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
class Employee { // employee class
public:
int employeeId;
string firstName;
string lastName;
string phoneNumber;
string emailAddress;
double salary;
Employee(int id, string first, string last, string phone, string email, double sal) // employee class constructor
:employeeId(id), firstName(first), lastName(last), phoneNumber(phone), emailAddress(email), salary(sal) {}
void display() { // used to display an object of the employee class
cout << "Id: " << employeeId << ", Name: " << firstName << " " << lastName
<< ", Phone Number: " << phoneNumber << ", Email: " << emailAddress
<< ", Salary: " << salary << endl;
}
};
class Node { // class for each node of the linked list
public:
Employee* info;
Node* next;
Node(Employee* emp) : info(emp), next(nullptr) {}
};
class LinkedList { //the link list class itself
private:
Node* head;
public:
LinkedList() : head(nullptr) {} // constructopr that initializes the list with no contents (empty)
void insert(Employee* emp) {
Node* newNode = new Node(emp);
if (head == nullptr || head->info->employeeId >= newNode->info->employeeId) {
newNode->next = head;
head = newNode;
}
else {
Node* current = head;
while (current->next != nullptr && current->next->info->employeeId < newNode->info->employeeId) {
current = current->next;
}
newNode->next = current->next;
current = current->next;
}
}
void display() { // function that displays the nodes of the link list
Node* current = head;
while (current != nullptr) {
current->info-> display();
current = current->next;
}
}
~LinkedList() {
Node* current = head;
while (current != nullptr) {
Node* next = current->next;
delete current;
current = next;
}
}
};
// Now that all three classes are created and the functions needed with them,
// we can now read from the file and add the employees to the linked list
void readFile(const string& filename, LinkedList& list) {
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error opening file: " << filename << endl;
return;
}
string line;
while (getline(file, line)) {
stringstream ss(line);
int id;
string firstName, lastName, phoneNumber, emailAddress;
double salary;
ss >> id >> firstName >> lastName >> phoneNumber >> emailAddress >> salary;
Employee* emp = new Employee(id, firstName, lastName, phoneNumber, emailAddress, salary);
list.insert(emp);
}
file.close();
}
int main() {
LinkedList list;
readFile("Week 1-employeeDataset_Project 1.txt", list);
list.display();
return 0;
}
A sample line from the input file looks like this.
12690 Glynis Filipson 539-917-2209 gfilipson56@liveinternet.ru 105401.55
I believe the main issue if anything may lie in the read file function (above).
>Solution :
Your insert function is wrong. In the else you have:
Node* current = head;
while (current->next != nullptr && current->next->info->employeeId < newNode->info->employeeId) {
current = current->next;
}
newNode->next = current->next;
current = current->next;
At the end of this nothing in the linked list points to newNode, meaning it wasn’t actually inserted. I can’t test this right now, but I believe the last line should instead be
current->next = newNode;
(which is similar to the body of the if, but with current->next instead of head)