【问题标题】:Unable to return struct value in c++无法在 C++ 中返回结构值
【发布时间】:2020-04-03 10:14:38
【问题描述】:

我有两个文本文件,Load function 将数据从两个文本文件传输到单个结构 (Employee emp[length]),其 const 长度为 2001。这是因为文本文件中有 2000 个员工详细信息。

将数据加载到 struct 后,我​​想使用 Select 函数搜索和显示员工数据。

系统将提示用户选择将用于搜索的员工属性和关键字。但是,我意识到我不能返回 struct(emp[i]) 或字符串值 (emp[i].empId)。它会提示一个错误说

访问冲突读取位置0x00D2C000

但是,我可以使用 cout 显示字符串值(emp[i].empId)。

我可以知道为什么我可以cout 字符串值但不能返回吗?

提前感谢您的帮助,并为我的英语不好感到抱歉。

const int length = 2001;

struct Employee {
string empId;
string dOB;
string height;
string weight;
string yrOfWork;
string salary;
string allowance;
string name;
string country;
string designation;
string gender;
string lvlOfEdu;
};

Employee emp[length];

void Load();
Employee Select(int k, string s, int c);


int main() {
bool quit = false;
int option;

while (quit != true) { //loop the program unless 7 is chosen
    Load();

    cout << "1. Add" << endl; //
    cout << "2. Delete" << endl;
    cout << "3. Select" << endl;
    cout << "4. Advanced Search" << endl;
    cout << "5. Standard Deviation" << endl;
    cout << "6. Average" << endl;
    cout << "7. Quit" << endl;

    cout << "Please key in an option: ";
    cin >> option;
    system("cls"); //to refresh the screen

    switch (option) {
    case 3: {
        int search;
        string key;

        cout << "1.  Employee ID" << endl;
        cout << "2.  Date of Birth" << endl;
        cout << "3.  Height" << endl;
        cout << "4.  Weight" << endl;
        cout << "5.  Years of Working" << endl;
        cout << "6.  Basic Salary" << endl;
        cout << "7.  Allowance" << endl;
        cout << "8.  Employee Name" << endl;
        cout << "9.  Country" << endl;
        cout << "10. Designation" << endl;
        cout << "11. Gender" << endl;
        cout << "12. Level of Education" << endl;

        cout << "Select By: ";
        cin >> search;
        cout << "Enter keyword: ";
        cin >> key;

        for (int i = 0; i < length; i++) {
            cout << Select(search, key, i).empId;
        }

        system("pause");
        system("cls");
        break;
        }
    }
}
}

Employee Select(int s, string k, int c) {
int result;
int i = c;

switch(s) {
case 1:

    result = emp[i].empId.find(k);
    if (result >= 0) {
        return emp[i];
    }

    break;
}
}

void Load() {
ifstream inFigures;
inFigures.open("profiles_figures.txt");
ifstream inWords;
inWords.open("profiles_words.txt");

if (inFigures.is_open()) {
    int i = 0;
    while (!inFigures.eof()) {

        inFigures >> emp[i].empId;
        inFigures.ignore();
        inFigures >> emp[i].dOB;
        inFigures.ignore();
        inFigures >> emp[i].height;
        inFigures.ignore();
        inFigures >> emp[i].weight;
        inFigures.ignore();
        inFigures >> emp[i].yrOfWork;
        inFigures.ignore();
        inFigures >> emp[i].salary;
        inFigures.ignore();
        inFigures >> emp[i].allowance;
        inFigures.ignore();
        i++;
    }
}
//inFigures.close();

if (inWords.is_open()) {
    int i = 0;
    while (!inWords.eof()) {

        getline(inWords, emp[i].name);
        getline(inWords, emp[i].country);
        getline(inWords, emp[i].designation);
        inWords >> emp[i].gender;
        inWords.ignore();
        inWords >> emp[i].lvlOfEdu;
        inWords.ignore();
        i++;
    }
}
//inWords.close();
}

【问题讨论】:

  • 1) 请提供您使用的输入。如果s != 1result &lt; 0,您认为您的Select 函数会返回什么? 2)另外,“_with a const length of 2001。这是因为文本文件中有2000个员工详细信息”的推理。没有意义。如果一个文件中正好有 2000 条员工记录,2000 的数组就足够了。可能出现问题的原因是您在读取文件时可能会遇到inWords.eof () you are using
  • 数组不需要额外的元素。我认为您将“C 字符串”数组与一般数组混淆了。

标签: c++ struct fstream


【解决方案1】:

我认为的主要问题是,如果Select 没有找到任何东西,你会返回什么?该函数应该返回一个员工。你可以有一个特殊的Employee 和一个无意义的empId(例如-1)来表明这一点,然后改变

for (int i = 0; i < length; i++)
{
    cout << Select(search, key, i).empId;
}

for (int i = 0; i < length; i++)
{
    Employee selected = Select(search, key, i);
    if (selected.empId != -1)
    {
        cout << Select(search, key, i).empId;
    }
}

或者,您可以更改 Select 函数,使其返回指针 Employee *,如果不匹配则返回 nullptr。那是

Employee* Select(int s, string k, int c)
{
    int result;
    int i = c;   // why not just use c directly? Or change the argument to int i?

    switch(s)
    {
    case 1:

        result = emp[i].empId.find(k);
        if (result >= 0)
        {
             return &emp[i]; // note taking address, could also write emp + i
        }

        break;   // don't need this with no further cases
     }

     return nullptr; // reached if no match above
}

后来

for (int i = 0; i < length; i++)
{
    Employee* selected = Select(search, key, i);
    if (selected != nullptr)
    {
        cout << Select(search, key, i)->empId;  // not pointer indirection
    }
}

确实,您可能想要返回 const Employee const*,但这是另一个话题。

另一种选择是让Select 在找不到任何东西时抛出异常,并将对Select(search, key, i); 的调用放在try .. catch 块中。我一般不喜欢对这样的控制流使用异常,但这是另一种方法。

【讨论】:

  • 你好,我用你的第一种方法解决了这个问题。但是,搜索过程需要相当长的时间,大约 2 分钟。是否认为程序循环遍历 2000 组数据的正常时间范围?
  • 不,这听起来很慢。
  • 有解决办法吗?
  • 您好,这里只是更新。原来我不小心在我的 Select 函数中插入了 Load 函数。只需从 Select 函数中删除 Load 函数即可解决问题。
猜你喜欢
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多