hello everyone. so im on my second semester of c++ and am having lot of problems as the class begins to involve more and more pointers(:@).

we were assigned a program that created a structure for course grades. it was asked that we dynamically allocated an array for the tests since there were to be several per student. i was able to somewhat figure everything out but when i run the program, it always crashes after the first test score.

needless to say i have no idea how to fix the problem.

im posting everything but the output. thanks in advance.

- Jessica :)

#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <math.h>
#include <string>

using namespace std;

struct StudentsGroup
{
string name;
int idnumber;
double *test;
double average;
char grade;
};

int main()
{
cout << showpoint << setprecision(2) << fixed;

int const NAME_LENGTH = 21;
int numStudents = 0;
int numTests = 0;
int index = 0;
int j = 0 ;

cout << "How many students are there? ";
cin >> numStudents;
cout << endl << "How many test scores are there? ";
cin >> numTests;

StudentsGroup student[numStudents];
student[numStudents].test = new double[numTests];




cout << endl << "Enter the students names, ID numbers, and test scores. " << endl << endl;

for (index = 0; index < numStudents; index++)
{
    
    cout << "Name of student: ";
    cin.ignore();
    getline(cin, student[index].name);
    
    cout << student[index].name << "'s ID number: ";
    cin >> student[index].idnumber;
        if (student[index].idnumber < 0)
           {
             cout << "You must enter a positive number" << endl ;
             cout << "Reenter " << student[index].name << "'s ID number: ";
             cin >> student[index].idnumber;
           }
    double totalTest = 0;  
    for (j = 0; j < numTests; j++)
    { 
      cout << "Number grade for test #"<< (j + 1) << ": "; ////// CRASHES -_-
      cin >> student[index].test[j];
          if (student[index].test[j] < 0 || student[index].test[j] > 100)
             {
               cout << "You must enter a number between 0 and 100" << endl ;
               cout << "Reenter number grade for test # "<< (j + 1) << ": ";
               cin >> student[index].test[j];
             }
      totalTest = totalTest + student[index].test[j];
      
      }
      student[index].average = totalTest / numTests;      
      
     if ( student[index].average >= 91 && student[index].average <= 100 )
          student[index].grade == 'A';
     if ( student[index].average >= 81 && student[index].average <= 90 )
          student[index].grade == 'B';
     if ( student[index].average >= 71 && student[index].average <= 80 )
          student[index].grade == 'C';
     if ( student[index].average >= 61 && student[index].average <= 70 )
          student[index].grade == 'F';
     if ( student[index].average <= 60 )
          student[index].grade == 'F';

Your problem is in line 34. Say there are 10 students, so on the line before you set aside an array of 10 (which has indexes from 0 to 9).
So next you are trying to create an array at student[10].test, which doesn't exist.

You must have a loop going from 0 to numStudents and initialize all of the test arrays in a row.

Your problem is in line 34. Say there are 10 students, so on the line before you set aside an array of 10 (which has indexes from 0 to 9).
So next you are trying to create an array at student[10].test, which doesn't exist.

You must have a loop going from 0 to numStudents and initialize all of the test arrays in a row.

that did the trick. thank you =]

This article has been dead for over six months. Start a new discussion instead.