I have an assigned program to retrieve values from the user adding them to a record structure (name, ID, test scores, Average and Letter Grade). The program is running as expected to retrieve the data from the user. However, I am attempting to display the record data to the console at the end and it is printing the last record values for the number of record that exist. I am trying to get my display function to start at the first record in the struct and print all that was entered. I am even wondering (since it is my first struct) that is it is setup correctly storing multiple records or if each entry is erasing the previous(?).
The following is the struct that is used:
struct StudentRecords //record structure
{
char name[256];
int id;
int scores[numb_scores];
double avg;
char ltr_grade;
};
..
and below is the function I am attempting to display the records:
void displayRecords( StudentRecords& s, int n ){
//Display name, id, average, and grade letter for each student.
cout<<fixed<<setprecision(2);
int spc = 10;
cout<<endl;
cout<<"Name"<<setw(spc+5)<<" ID "<<setw(spc)<<"Avg"<<setw(spc+4)<<"Grade"<<endl;
cout<<"----"<<setw(spc+5)<<"----"<<setw(spc)<<"---"<<setw(spc+4)<<"-----"<<endl;
for(int i= 0; i<n; i++){
cout<<s.name<<setw(spc+4)<<s.id<<setw(spc)<<s.avg<<setw(spc+4)<<s.ltr_grade<<endl;
}
cout<<endl;
}
If you write all record to the display then you should modify your code in the following:
1 2 3 4 5 6 7 8
void displayRecords( StudentRecords* s, int n ){
//Display name, id, average, and grade letter for each student.
...
for(int i= 0; i<n; i++){
cout<<s[i].name<<setw(spc+4)<<s[i].id<<setw(spc)<<s[i].avg<<setw(spc+4)<<s[i].ltr_grade<<endl;
}
...
}
I don't know about setw... but you should use pointer to write all record.