As Narue so kindly pointed out, you went past the array, that gets you into garbage values. There is also a casting problem with the variable validate. You may want to add checks for non-numeric input, or you get into strange loops! I don't have VC++ up, so I tested the corrected code on Dev-C++
[php]
#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; // needs check for non-numeric input
cout <<"how many scores for each student: ";
cin >> number_scores; // needs check for non-numeric input
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);
system("PAUSE"); // wait, needed for Dev-C++
return 0;
} // end of main
////////////////////////////////////////////////
//////// functions definition /////////////////
void allocate_memory(int number_students , int number_scores)
{
student = new person[number_students];
// zero base this, array goes only to number_students - 1 !!!!!
for (int x = 0 ; 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[i];
validate = (int)validate_score(student[k].testscores[i]); // cast to int from float!!!!
if (!validate)
{
i = i - 1;
cout <<"Invalid score value, Re-Enter Score : " << endl;
}
else
{
total = total + student[k].testscores[i];
}
} // 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 = 0 ; x < number_students; x++) // zero base this!!!!
{
delete [] student[x].testscores ;
}
delete [] student;
return;
} // end of release memory
////////////////////////////////////////
[/php]
Now finish your averaging and grade math etc.
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
Offline 5,789 posts
since Oct 2004