为什么我不能在结构的对象数组中插入值? [英] Why can't I insert values in an array of objects of a structure?
问题描述
我有这段代码,-collection-是我的fstream对象,-students-是我制作的结构。我创建了-file [ctr] - 这是一个学生对象的数组,当我构建并运行它时,我使用的IDE是Code :: Blocks不会给我错误。错误消息[fileprocessing.exe已停止工作。填写名字后出现问题导致程序停止正常工作......等。知道我的代码可能有什么问题吗?
I have this block of code, -collection- is my fstream object and -students- is a structure I made. I made -file[ctr]- which is an array of objects of students, when I build and run it, the IDE I use which is Code::Blocks don't give me errors. The error message [fileprocessing.exe has stopped working. A problem caused the program to stop working correctly...etc.] appears after I fill up firstname. Any idea to what may be wrong with my code?
if(choice2=='A'||choice2=='a'){
students file[ctr];
cout<<"Student ID: ";
cin>>file[ctr].studentID;
cout<<"Surname: ";
cin>>file[ctr].surname;
cout<<"Firstname: ";
cin>>file[ctr].firstname;
cout<<"Birthdate: ";
cin>>file[ctr].birthdate;
cout<<"Sex: ";
cin>>file[ctr].sex;
fstream collection(filename, std::fstream::in | std::fstream::out | std::fstream::app);
collection<<ctr
<<"\t"<<file[ctr].studentID
<<"\t"<<file[ctr].surname
<<"\t"<<file[ctr].firstname
<<"\t"<<file[ctr].birthdate
<<"\t"<<file[ctr].sex
<<endl;
collection.close();
ctr++;
system("cls");
}
我的尝试:
What I have tried:
int ctr=1;
if(choice2=='A'||choice2=='a'){
students file;
cout<<"Student ID: ";
cin>>file.studentID;
cout<<"Surname: ";
cin>>file.surname;
cout<<"Firstname: ";
cin>>file.firstname;
cout<<"Birthdate: ";
cin>>file.birthdate;
cout<<"Sex: ";
cin>>file.sex;
fstream collection(filename, std::fstream::in | std::fstream::out | std::fstream::app);
collection<<ctr<<"\t"<<file.studentID
<<"\t"<<file.surname
<<"\t"<<file.firstname
<<"\t"<<file.birthdate
<<"\t"<<file.sex
<<endl;
collection.close();
ctr++;
system("cls");
}
简单,我刚删除了[ctr]并且效果很好,但我知道这会导致当我在系统的其他部分工作时,我遇到了问题。我必须创建一个对象数组,因为我在一组文本文件中有一组学生数据,它们不应该是学生对象...请帮助!
Simple, I just removed the [ctr]s and it works well, but I know it's going to cause me problems when I work on the other parts of my system. I have to make an array of objects because I have a collection of student data in a collection of text files and they shouldn't be of a students object... Please help!
推荐答案
students file[ctr];
cout<<"Student ID: ";
cin>>file[ctr].studentID;
这是错的;你正在声明一个学生
对象的数组,并立即使用count作为数组的索引(有效数组索引从0到count - 1)。因此,在这种情况下,它意味着您创建的结构将覆盖您的内存并最终导致一些随机事件,例如应用程序崩溃。你应该这样做:
That is wrong; you are declaring an array of students
objects, and immediately using the count as an index into the array (valid array indices go from 0 to count - 1). So in this case it means that the structure you create will be overwriting your memory and ultimately cause some random event, such as an application crash. You should do it like:
int count; // the number of elements in the array
// initialise count somehow
// declare the array of 'count' elements
students file[count];
// now use a loop to fill the array
int ctr;
for (ctr = 0; ctr < count; ++ctr)
{
cout<<"Student ID: ";
cin>>file[ctr].studentID;
// ... etc
} // end of for loop
这篇关于为什么我不能在结构的对象数组中插入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!