Click here to Skip to main content
15,889,615 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C++
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:

C++
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");
}


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!
Posted
Updated 20-Aug-16 1:27am
v2

1 solution

C++
students file[ctr];
cout<<"Student ID: ";
cin>>file[ctr].studentID;

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:
C++
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
 
Share this answer
 
Comments
mariaclara 21-Aug-16 4:20am    
Thank you so much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900