Hello,
So I'm working on an assignment and I'm stuck on a few things. I need to create a linked list to add employees and their data to. The first part is adding an employee with their data. I thought I had this and it's saying the variable isn't initialized. Here is the function:
int numberofEmployees = 1;
string s;
double b;
do {
cout << " \n What would you like to do? \n";
cout << " 1. Add New Employee\n 2. Print all Employees and their data\n 3. Print Employees with 0 Dependents\n 4. Give employees that make less than $50000 a 10% raise\n 5. Add dependent to certain employee\n 6. Exit\n";
cin >> choice;
switch (choice)
{
case 1: cout << " \nCreating a new employee...\n";
cout << "Enter employee name: ";
cin >> s;
cout << "Enter the number of dependents ";
InsertBeginning(numberofEmployees, s, d, L);
numberofEmployees++;
break;
It's saying that the variable d isn't initialized.
The last problem I'm having is printing all the employees with 0 dependents. I know I would have to use my print function but I'm not sure how.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void Print(Node* L)
{
Node* P = L; // P points to the empty header
cout << "\n List: ";
while (P->Next != NULL)
{
P = P->Next; // move one node forward
cout << P->Num << " " << P->Name << " " << P->Dependents << endl; // print out only the element part
} // stop when there is no next node
cout << endl;
}
I don't see where you initially define d in your input/output snippet of code.
You need to make sure you've given d a proper value before calling line 16 (InsertBeginning).
void Print(Node* L, bool only_print_zero_dep)
{
Node* P = L; // P points to the empty header
cout << "\n List: ";
while (P->Next != NULL)
{
P = P->Next; // move one node forward
if (only_print_zero_dep && P->Dependents != 0)
continue;
cout << P->Num << " " << P->Name << " " << P->Dependents << endl; // print out only the element part
} // stop when there is no next node
cout << endl;
}
Also should be safe and do check to make sure L itself isn't null.