我不明白爲什麼我進入主函數時立即得到堆棧溢出。我應該從文本文件中讀取並做一些處理。有人可以向我解釋原因並提出解決方法嗎?不知道爲什麼我得到一個堆棧溢出
#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