簡體   English   中英

無法在 c++ 中返回結構值

[英]Unable to return struct value in c++

我有兩個文本文件,並且Load function將數據從兩個文本文件傳輸到單個結構( Employee emp[length] ),其 const 長度為 2001。這是因為文本文件中有 2000 個員工詳細信息。

將數據加載到結構后,我想使用 Select function 搜索和顯示員工數據。

系統將提示用戶選擇將用於搜索的員工屬性和關鍵字。 但是,我意識到我不能返回一個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();
}

我認為的主要問題是,如果Select沒有找到任何東西,你會返回什么? function 應該返回一名員工。 你可以有一個特殊的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 function 以便它返回指針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塊中。 我通常不喜歡對這樣的控制流使用異常,但這是另一種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM