#include <iostream>
#include <stdio.h>
#include <iomanip>
using namespace std;
struct person
{
char name[15];
int Idnum;
int *testscores;
float average;
char grade;
} *student, freshman;
//////////// function prototype ///////////
void allocate_memory(int number_students , int number_scores);
void get_data(int number_students ,int number_scores);
float validate_score(int score);
void print_results(int number_students ,int number_scores);
void release_memory(int number_students );
////////////////////////////////////////////
int main()
{
int number_students , number_scores;
cout <<"How many students in this class: ";
cin >> number_students;
cout <<"how many scores for each student: ";
cin >> number_scores;
allocate_memory(number_students , number_scores);
get_data(number_students , number_scores);
// average = average_score();
print_results(number_students , number_scores);
release_memory(number_students);
return 0;
} // end od main
////////////////////////////////////////////////
//////// functions definition /////////////////
void allocate_memory(int number_students , int number_scores)
{
student = new person[number_students];
for (int x = 1 ; x <= number_students; x++)
{
student[x].testscores = new int[number_scores];
}
} // end allocate memory
//////////////////////////////////////
void get_data(int number_students ,int number_scores)
{
float total = 0;
int validate = 0;
for (int k = 0; k < number_students ; k++)
{
student[k].average = 0;
cout <<"What is the name of student # " << (k+1) << ": ";
cin >> student[k].name;
cout <<"What is the ID number of " << student[k].name << " : " ;
cin >> student[k].Idnum ;
for (int i=0 ; i < number_scores ; i++)
{
cout <<"What is score # " << (i+1) << " for " << student[k].name << " : " ;
cin >> student[k].testscores;
validate = validate_score(student[k].testscores);
if(!validate)
{
i = i - 1;
cout <<"Invalid score value, Re-Enter Score : " << endl;
}
else
{
total = total + student[k].testscores;
}
} // end of inner for loop ( looping through scores)
student[k].average = (total / number_scores);
} //end of outer for loop (looping through all students)
} // end of get_data
/////////////////////////////////////
float validate_score(int score)
{
if (score < 0 || score > 100)
{
return 0;
}
else
{
return 1;
}
}// end of validate
//////////////////////////////////////
void print_results(int number_students ,int number_scores)
{
for (int k = 0; k < number_students; k++)
{
for (int j=0 ; j < number_scores ; j++)
{
cout << "using structure variable = " << student[k].testscores[j] << endl;
}
}
}//end of print data
///////////////////////////////////////
void release_memory(int number_students)
{
for (int x = 10 ; x < number_students; x++)
{
delete [] student[x].testscores ;
}
delete [] student;
return;
} // end of release memory
////////////////////////////////////////
sorry for not using tags, I need to learn how to use them. Tha above code suppose to do the followings:
1) ask the user to to select how many students and how many scores to enter for each student.
2) the structure has a pointer to an array where the scores need to be stored for each student.
3) allocate memry for the students and allocate memory for the scores arrays.
4) print out the contents of the arrays.
I do not want to use classes or vector , only memory allcation to the above and basic structure.
When the program reaches the point where to input the first score, it gives an error??? how to allocate memory for the above problem corectly??? please help. I have spent alot of time tweek the code but no use(the code compiles in vc++ without any problem, but fails during running.)