简体   繁体   English

访问结构时出现分段错误

[英]Segmentation fault when accessing a structure

The program works all the way up until it checks for the name the user enters. 该程序一直运行到检查用户输入的名称为止。 When you enter the name you wish to search for in the array of structures that have been imported from a file full of customer info) it comes back segmentation fault core dumped. 当您输入要在从完整的客户信息文件中导入的结构体数组中搜索的名称时,它将返回分段故障核心转储。 This puzzles me. 这让我感到困惑。

#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;

struct AccountsDataBase{

        char name[50];
        string email;
        long int phone;
        string address;
};


#define MAX 80

AccountsDataBase * account = new AccountsDataBase[MAX];


void readIn(ifstream& file){
        int i=0;
        while(!file.eof()){
                file >> account[i].name >> account[i].email >> account[i].phone >> account[i].address;
        }
}

void getAccount(){

        char userPick[50];
        char streamName[50];

        cout << " What account will we  be using? " << endl;

        cin.getline(streamName, 50);

        for(int i=0; strcmp(account[i].name, streamName)!=0; i++){
                if( strcmp(account[i].name, streamName)==0){
                        cout << "\n\n FOUND IT!! \n\n";
                        cout << account[i].name << "\n" << account[i].email << "\n" << account[i].phone << "\n" << account[i].address << endl;
                }
        }
}

int main(){
        ifstream file;
        file.open("2.dat"); //opens data account records text
        readIn(file);
        getAccount();
        delete account;
        return 0;
}

Your loop keeps reading everything into the initial element of the array: 您的循环不断将所有内容读入数组的初始元素:

while(!file.eof()){
    file >> account[i].name >> account[i].email >> account[i].phone >> account[i].address;
}  

because the value of i is never incremented. 因为i的值永远不会增加。 You can convert this to a for loop, like this: 您可以将其转换为for循环,如下所示:

for (count = 0 ; count < MAX && !file.eof() ; count++) {
    file >> account[count].name >> account[count].email >> account[count].phone >> account[count].address;
}

Note that I changed i to count : 请注意,我将i更改为count

AccountsDataBase * account = new AccountsDataBase[MAX];
int count = 0;

This will help you solve another problem - determining when the array ends in the getAccount function. 这将帮助您解决另一个问题-确定数组何时在getAccount函数中结束。 Currently, you assume that the record is always there, so the outer loop keeps going on. 当前,您假设记录始终存在,因此外循环继续进行。 Now that you have count , you could change the loop like this: 现在您有了count ,您可以像这样更改循环:

for(int i=0; i < count && strcmp(account[i].name, streamName)!=0; i++){
    if( strcmp(account[i].name, streamName)==0){
        cout << "\n\n FOUND IT!! \n\n";
        cout << account[i].name << "\n" << account[i].email << "\n" << account[i].phone << "\n" << account[i].address << endl;
        break;
    }
}
if (i == count) {
    cout << "Not found." << endl;
}

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

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