Trying to display all record structure values via cout..

Hi all,

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. 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;
}

Thanks in advance!

Geoff-

How are you calling displayRecords?
Where are you storing all the inputs?
Here's the main function whereas displayRecords is called after the do while loop:

void main(){
//declare and allocate memory for record structure
StudentRecords s;
int n = 0;
char a = 'y';

do
{
if(a=='y' || a=='Y' ){

//s.id = 1000 + n++; requirement to ask for id from user
n =++n; //keeping increment
getName(s, n);
getId(s);
getScore(s);
getAvg(s);
getLtrGrade(s);
}
if(n>0){
cout<<"Enter another Student Record? (y/n)";
cin>>a;
}
cin.ignore();

}while(addStudent(a));

if(n>=1){
cout<<endl;
displayRecords(s, n);
}
}//end main


Having this be my first record structure assignment and was taught as preferred over arrays (and I agree thus far) I would presume the inputs are stored in the record structure declared as "s" until the program exits.
You aren't saving all the input. Each overwrites the previous one.
Having this be my first record structure assignment and was taught as preferred over arrays
What were you taught to use instead of arrays?

Please use [code][/code] tags
Ok I researched a bit and found that I'm to add a subscript to the struct storing the values. However, It seems to require a static constant at the file level. So how would the number of records change as the user needed to add more values than initially allowed?

Thanks again!!

Geoff-

(btw this assignment was to illustrate the convenience of passing references to record contruct vs. using arrays - reducing complexities and overhead time)
Topic archived. No new replies allowed.