c++ - C++中从文件中读取数据到队列中

标签 c++ file input queue

我在弄清楚如何将我的输入数据放入我的队列中时遇到问题...我非常接近让它正常工作。

我知道我只是对事情的运作方式感到困惑。我已经使用示例代码和我的说明提出了一个似乎正常工作的工作程序(除了实际上没有将我的输入文件数据放入队列中)。我绕过了我试图为此做的功能。除此之外,我还试图编写一个函数来从队列中删除一名员工(我认为这确实有效),但我不确定我是否能够正确...

我已经 10 多年没有上过编程课了,真的很想得到任何帮助,以了解我在做什么,并将该死的数据放入队列。

下面是我的主要驱动文件。如果需要,我将提供我的头文件代码。在此先感谢您提供的任何帮助。

//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;
//}

}

这是LinkedQ的实现文件:

//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;
}

这是 Employee 实现文件:

//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;
}

最佳答案

我认为loadFile的参数应该是LinkedQ类型,如果我没理解错的话,就是队列类/结构,而empIn 变量的类型应为 Employee

编辑:

您在 empList 对象上调用的方法应该是 Enqueue,而不是 addEmp

关于c++ - C++中从文件中读取数据到队列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52359135/

相关文章:

javascript - 搜索输入字段以搜索 JavaScript 数组 - 如何验证空输入框

python - 为什么一些在线 Python 编译器不允许我输入输入?

Python 子文件夹列表

使用不同模式下的文件进行 C 编程?

python - 迭代二进制文件的惯用方法是什么?

c++ - 3D 数组到 3D std::vector

winapi - 同时多键按下释放延迟

c++ - 读取二进制返回垃圾

c++ - 如何使用 C/C++ 在 Windows 7 中使麦克风静音?

c++ - 返回私有(private)类成员是否比使用结构并直接访问该变量慢?