#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.)

Recommended Answers

All 3 Replies

Well your first problem is that you didn't use code tags
Read This

Using global variables makes your code harder to follow. But I immediately found this error:

for (int x = 1 ; x <= number_students; x++)

Why are you starting with 1 and ending at number_students when the array only goes from 0 to number_students - 1?

This is suspicious as well:

for (int x = 10 ; x < number_students; x++)

Why are you starting with 10?

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++

#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
////////////////////////////////////////

Now finish your averaging and grade math etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.