Reading data from file into queue in c++

4087 views c++
9

I am having trouble figuring out how to get my input data into my queue... I am so close to getting this to work right.

I know I am just confused about how things are working. I have used example code and my instructions to come up with a working program that appears to be working correctly (other than not actually putting my input file data into the queue). I bypassed the function I was trying to make for this. In addition to this, I was trying to write a function to remove an employee from the queue (which I think does work), but I am not sure I was able to get it right...

I have not taken a programming class for over 10 years and really would love to get any help in understanding what I am doing and getting that darn data into the queue.

Below is my main driver file. I will provide my header file code if needed. Thanks in advance for any help you can provide on this.

//Program Assignment #3 
//Creates Queue as a Linked Structure

#include<iostream>
#include<string>
#include<fstream>
#include"Employee.h"
#include"LinkedQ.h"

using namespace std;

struct Node
{
    LinkedQ nodeQ;
    Employee EmpNumber;
    Employee LastName;
    Employee FirstName;
    Employee ServiceYears;
};
void loadFile(LinkedQ &);
void addEmp(LinkedQ &);
void delEmp(LinkedQ &);

int main()
{
LinkedQ empList;
int choice;

int numIn, yearsIn;
string LastName;
string FirstName;
LinkedQ empIn;
ifstream input;

input.open("Employee.txt");

while (input)
{
    input >> numIn >> LastName >> FirstName >> yearsIn;
    if (input)
    {
        cout << "this is where we load data from the file into the queue\n";
        system("pause");
        //empIn.Enqueue(numIn, LastName, FirstName, yearsIn);
        //empList.addEmp(empIn);
    }
}

input.close();

do
{
    //display menu
    system("cls");
    cout << "\t\tMenu: \n" 
        << "\t1. Add Employee\n"
        << "\t2. Remove Employee\n" 
        << "\t3. Count of Employees\n" 
        << "\t4. Quit\n\n";
    cout << "Enter your choice and press return: ";

    cin >> choice;

    switch (choice)
    {
    case 1:
        addEmp(empList); // call to function to add an employee to the queue
        break;

    case 2:
        delEmp(empList); // call to fucntion to remove an employee from the queue
        break;

    case 3:
        cout << endl << "Count of Employees: "
            << empList.GetLength() << endl;    // See how many employees are in the queue
        system("pause");
        break;

    case 4:
        cout << "End of Program";           // End Program
        break;

    default:                                
        cout << "Not a valid choice!" << endl;
        cout << "Choose Again.";                    // Handling incorrect inputs
        system("pause");
        break;
    }
} while (choice != 4);      // If choice is not 4, continue running program

return 0;
}

//***********************************
//Loads the file (having trouble figuring out how to implement this part)
//***********************************
void loadFile(Employee &empList)
{
int numIn, yearsIn;
string LastName;
string FirstName;
LinkedQ empIn;
ifstream input;

input.open("Employee.txt");

while (input)
{
    input >> numIn >> LastName >> FirstName >> yearsIn;
    if (input)
    {
        cout << "this is where we load data from the file into the queue";
        //empIn.setFields(numIn, LastName, FirstName, yearsIn);
        //empList.addEmp(empIn);
    }
}

input.close();
}

//***************************************
//add an employee
//***************************************
void addEmp(LinkedQ &empList)
{
Employee newEmp;

newEmp.user();

empList.Enqueue(newEmp);
}

//****************************************
//remove a employee
//****************************************
void delEmp(LinkedQ &empList)
{
Employee EmpToRemove;
int empNum;
//  bool successful;

cout << "Please enter EMPLOYEE NUMBER of employee to remove:";
cin >> empNum;

EmpToRemove.setEmpNumber(empNum);

empList.Dequeue(EmpToRemove);

//successful = empList.Dequeue(EmpToRemove);

//if (successful == true)
//{
    cout << "Removed" << endl << endl;
    system("pause");
//}
//else
//{
//  cout << "Emp Not found" << endl << endl;
//}

}

Here is the LinkedQ implementation file:

//LinkedQ class

#include "LinkedQ.h"
#include <cstddef>
#include <new>

struct NodeType
{
Employee info;
NodeType* next;
};

LinkedQ::LinkedQ(void)
{
newNode = nullptr;
front = NULL;
rear = NULL;
length = 0;
}

void LinkedQ::MakeEmpty()
{
NodeType* tempPtr;

while (front != NULL)
{
    tempPtr = front;
    front = front->next;
    delete tempPtr;
}

rear = NULL;
}

LinkedQ::~LinkedQ(void)
{
MakeEmpty();
}

bool LinkedQ::IsFull(

answered question

1 Answer

8

I think the parameter of loadFile should be of type LinkedQ, which, if I understand it correctly, is the queue class/struct, and the empIn variable should be of type Employee.

Edit:

The method you call on the empList object should be Enqueue, instead of addEmp.

posted this

Have an answer?

JD

Please login first before posting an answer.