【发布时间】:2014-05-31 15:59:59
【问题描述】:
程序一直运行,直到它检查用户输入的名称。当您输入您希望在从充满客户信息的文件中导入的结构数组中搜索的名称时,它会返回分段错误核心转储。这让我很困惑。
#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;
}
【问题讨论】:
-
嗯,你先在调试器中单步调试代码了吗?
-
@colin 不需要第 # 行,除非您的错误指示特定行
-
@OldProgrammer 我正在使用 vim 编辑器,并给了我一个完整的内存映射以及一个说无效指针的声明,但作为一个只有一年的程序员,我对如何回溯这个十六进制一无所知# 回到我的源代码。
-
我建议花一些时间学习调试技能。它将为您省去很多麻烦。
标签: c++ arrays segmentation-fault structure core