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

时间:2015-12-03 20:31:39

标签: c++ arrays class

我正在从文件中读取员工信息,如下所示:

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

其中P表示打印,E表示全部,D表示删除,Q表示退出。删除内容时,下面的数字" D",是在数组中搜索的ID。

当我尝试打印多个员工或删除现有员工时,我遇到了无限循环。以下是我的代码:

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

1 个答案:

答案 0 :(得分:0)

一旦提取到达流的末尾,std::getline可能无法清除传递的字符串,因此code保持原样...但是我看到了&{ #39;不是......

主要问题是您不检查是否有从输入流中提取的内容。

编写循环的更好方法是:

// 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
{
    ...
}