简体   繁体   中英

Reading data from file into queue in c++

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() const
{
NodeType* location;
try
{
    location = new NodeType;
    delete location;
    return false;
}
catch (std::bad_alloc exception)
{
    return true;
}
}

bool LinkedQ::IsEmpty() const
{
return (front == NULL);
}

void LinkedQ::Enqueue(Employee newItem)
{
if (IsFull())
    cout << "Queue is Full";
//  throw FullQueue();
else
{
    NodeType* newNode;

    newNode = new NodeType;
    newNode->info = newItem;
    newNode->next = NULL;
    if (rear == NULL)
    {
        front = newNode;
    }
    else
    {
        rear->next = newNode;
    }
    rear = newNode;
    length++;
}
}

void LinkedQ::Dequeue(Employee& item)
{
if (IsEmpty())
{
    //throw EmptyQueue();
    cout << "Queue is empty";
}
else
{
    NodeType* tempPtr;

    tempPtr = front;
    item = front->info;
    front = front->next;
    if (front == NULL)
    {
        rear = NULL;
    }
    delete tempPtr;
    length--;
}
}

int LinkedQ::GetLength() const
{
return length;
}

And here is the Employee implementation file:

//employee Class

#include"Employee.h"

//Constructor
Employee::Employee()
{
EmpNum = 0;                                             
}

//setters

void Employee::setEmpNumber(int eNum)
{
EmpNum = eNum;
}

void Employee::setEmpName(string LName)
{
LastName = LName;
}

void Employee::setEmpFirstName(string FName)
{
FirstName = FName;
}

void Employee::setYearsService(int years)
{
YearsService = years;
}

void Employee::setFields(int num, string LN, string FN, int years)
{
EmpNum = num;
LastName = LN;
FirstName = FN;
YearsService = years;
}

void Employee::user()                                                 
{
string inputString;
int intNumber;

cout << "Employee Number ";
cin >> intNumber;
while (intNumber <= 0)                                                  
{
    cout << "Employee Number "; 
    cin >> intNumber;
}
EmpNum = intNumber;

cout << "Last Name: ";
cin >> inputString;
LastName = inputString;

cout << "First Name: ";
cin >> inputString;
FirstName = inputString;

cout << "Years of Service: ";
cin >> intNumber;
while (intNumber < 0)                                                   
{
    cout << "Years of Service ";
    cin >> intNumber;

}
cout << endl;
YearsService = intNumber;

}

//getters
const int Employee::getEmpNumber()
{
return EmpNum;
}

const string Employee::getLastName()
{
return LastName;
}

const string Employee::getFirstName()
{
return FirstName;
}

const int Employee::getYearsService()
{
return YearsService;
}

//overloads

bool Employee::operator == (const Employee &right)
{
bool status;
if ( EmpNum == right.EmpNum)
    status = true;
else
    status = false;
return status;
}

bool Employee::operator != (const Employee &right)
{
bool status;

if (EmpNum != right.EmpNum)
    status = true;
else
    status = false;
return status;
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM