简体   繁体   English

从文件中读取信息后,发生无限循环

[英]After reading in information from a file, an infinite loop occurs

I am reading employee information from a file, which looks like the following: 我正在从文件中读取员工信息,该文件如下所示:

I
20017
Boris         
Pallares      
Board Room      
President       
550450.5
P
E
D
20017
P
E
Q

where P is for printing, E is for all, D is for delete, and Q is for quit. 其中P用​​于打印,E用于所有,D用于删除,Q用于退出。 When deleting something, the number following, "D", is the ID that is searched for in the array. 删除某些内容时,“ D”后面的数字是在数组中搜索的ID。

I am running into an infinite loop when I try to print more than one employee or delete an existing employee. 当我尝试打印多个员工或删除现有员工时,我陷入无限循环。 The following is my code: 以下是我的代码:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctype.h>
#include <string>
using namespace std;


ifstream input ("DATA7A.txt", ios::in);
ofstream output ("Output.txt",ios::out);

struct EmployeeType
{
int ID;
string fName;
string lName;
string dept;
string title;
float pay;
};

class EmployeeClass
{
public:
void GetInfo(ifstream &, int &);
string getfName(int num) {return Emp[num].fName;};
int getID(int num) {return Emp[num].ID;};
string getlName(int num) {return Emp[num].lName;};
string getDept(int num) {return Emp[num].dept;};
string getTitle(int num) {return Emp[num].title;};
float getPay(int num) {return Emp[num].pay;};
void PrintInfo(ofstream &, ifstream &, int &);
void Delete(ifstream &, int &);
private:
EmployeeType Emp[50];
static float TotalPayroll;
};

void Header(ofstream &);
void Footer(ofstream &);

float EmployeeClass::TotalPayroll = 0.0;

int main()
{
EmployeeClass Employee;
int EU = 0;
int IDNum = 0;
string code;
string pCode;
output.setf(ios::fixed);
output.precision(2);
Header(output);
getline(input,code); //read in first transaction code
while(code != "Q" || code!="D")
{
    if(code == "D")
    {
        Employee.Delete(input,EU);
    }
    if(code == "I")
    {
        Employee.GetInfo(input, EU);
        EU++;
    }
    else if(code == "P" || code == "p")
    {
        Employee.PrintInfo(output, input, EU);
    }
    getline(input,code); //read in next transaction code
}
Footer(output);
return 0;
}
void EmployeeClass::GetInfo(ifstream &input, int &EU)
{
input >> Emp[EU].ID;
input >> ws;
getline(input,Emp[EU].fName);
input >> ws;
getline(input,Emp[EU].lName);
input >> ws;
getline(input, Emp[EU].dept);
input >> Emp[EU].title;
input >> Emp[EU].pay;
return;
}

void EmployeeClass::PrintInfo(ofstream &output, ifstream &input, int &EU)
{
string pCode;
input >> pCode;
if(pCode == "E")
{
    int i;
    for(i=0;i<EU;i++)
    {
    output << setw(5) << Emp[i].ID;
    output << setw(5) << " "<< Emp[i].fName;
    output << setw(5) << " "<< Emp[i].lName;
    output << setw(5) << " "<< Emp[i].dept;
    output << setw(5) << " "<< Emp[i].title; 
    output << setw(5) << " "<< Emp[i].pay << endl;
    }
}
else if(pCode == "S")
{
    int search;
    input >> search;
    int i;
    for(i=0;i<EU;i++)
    {
        if(search == Emp[i].ID)
        {
        output << setw(5) << Emp[i].ID;
        output << setw(5) << " "<< Emp[i].fName;
        output << setw(5) << " "<< Emp[i].lName;
        output << setw(5) << " "<< Emp[i].dept;
        output << setw(5) << " "<< Emp[i].title; 
        output << setw(5) << " "<< Emp[i].pay << endl;
        }

    }
}
return;
}

void EmployeeClass::Delete(ifstream &input, int &EU)
{
int IDSearch;
int i;
input >> IDSearch;
for(i=0;i<EU;i++) 
{
    if(Emp[i].ID == IDSearch)
    {
        while(i<EU)
        {
            Emp[i].fName = Emp[i+1].fName;
            Emp[i].lName = Emp[i+1].lName;
            Emp[i].dept = Emp[i+1].dept;
            Emp[i].title = Emp[i+1].title;
            Emp[i].pay = Emp[i+1].pay;
            i++;
        }
        EU--;
    }
}
return;
}

It might be that std::getline doesn't clear the passed string once the extraction reaches the end of the stream, so code is left as it was before... but I see that's not... 一旦提取到达流的末尾,可能std::getline不会清除传递的字符串,因此code保留了以前的样子……但是我看到的不是……

The main issue is that you don't check if there was something extracted from the input stream. 主要问题是您不检查是否从输入流中提取了某些内容。

A better way to write the loop would be: 编写循环的更好方法是:

// make use of the return value of std::getline, pretty idiomatic
while(getline(input,code) && (code != "Q" || code != "D")) // only one getline, remove the other calls
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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