我不明白为什么我进入主函数时立即得到堆栈溢出。我应该从文本文件中读取并做一些处理。有人可以向我解释原因并提出解决方法吗?不知道为什么我得到一个堆栈溢出
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX=100;
enum countrytype{S,F};
struct dob
{
int day;
int month;
int year;
};
struct Local
{
char country[MAX];
char gender[MAX];
char name[MAX];
dob birthday;
int noofmod;
char mod[MAX][MAX];
int mark[MAX];
};
struct Foreign
{
char country[MAX];
char gender[MAX];
char name[MAX];
dob birthday;
int noofmod;
char mod[MAX][MAX];
int mark[MAX];
};
union Student
{
Local localstudent;
Foreign foreignstudent;
};
struct UOWstudent
{
countrytype ct;
Student st;
};
void readfile(ifstream &read,UOWstudent noofstudent[MAX]);
int main()
{
UOWstudent noofstudent[MAX];
ifstream read;
readfile(read,noofstudent);
cout<<endl
<<noofstudent[0].st.foreignstudent.country
<<endl
<<noofstudent[0].st.foreignstudent.gender
<<endl
<<noofstudent[0].st.foreignstudent.name;
system("PAUSE");
}
void readfile(ifstream &read, UOWstudent noofstudent[MAX])
{
int i=0;
char country;
char filename[MAX];
cin>>filename;
read.open(filename);
read>>country;
/*if (country =='F')
{
read.getline(noofstudent[i].st.foreignstudent.country,MAX);
read>>noofstudent[i].st.foreignstudent.gender;
read.getline(noofstudent[i].st.foreignstudent.name,MAX);
}
else
read.getline(noofstudent[i].st.foreignstudent.country,MAX);*/
}
这是我的文本文件
F South Korea
Male Psy Park Jae Sang
31 - 12 -1977
3 CSCI114 55 CSCI103 44 GangNam
您的代码将是这样更易于管理的,如果你用'的std :: string'。 – chris
我没有在我的系统上发生堆栈溢出。你确定你正在运行最新版本的可执行文件吗?你能给我们堆栈跟踪吗? – templatetypedef
@chris我无法使用字符串类 – Computernerd