for(int i = 0; i < 2; i++)
{
cout<<users[i].name << " " << users[i].birthYear << " " << users[i].major <<endl;
}
Looks incoorect. How are you so sure that users contain atleast two elements. If getline
fails because of badfile or first line suggests only 1 record, you will get above exception.
You should change the loop to
// Initialize size with 0 before while(getline) loop
for(int i = 0; i < size; i++)
{
cout<<users[i].name << " " << users[i].birthYear << " " << users[i].major <<endl;
}
Also below lines of code look problematic
if(count == 0)
{
users[index].name = buffer;
count++;
}
if(count == 1)
{
ss >> users[index].birthYear;
count++;
}
if(count == 2)
{
users[index].major = buffer;
count = 0;
index++;
}
When count is 0, it would go into first if condition and get incremented. Then condition count == 1
would become true and you will visit next 2 conditions also. You should replace next 2 if
conditions with else if
or a switch
with break
statements to see the intended behavior.
Also it would be a good practice to free users after you are done with it.