I normally don't try to get outside help on homework that is still due, but I've kinda' hit a wall on this one.

I'm trying to write a program that dynamically creates a list of scores for a group of students. It has to ask the user how many students (s)he wishes to process and how many tests scores there are to be. It then must dynamically create an array of structures for the student info and a second array to hold the test scores for each student.

The instructions are very clear that the tests must be stored in a dynamically allocated array, rather than using vectors.

I figured the best way to do this would be to use a 2-D array, but...

Well, I guess it's easier to show you than tell you. (The problem seems to be at line 60)

Here is a segment of my main function:

``````// Peter Kusen
// Date 2/15/2012
// This program will allow the user to set up a list of students, track the test scores of each student individually, average the test scores of each student individually, and call up any student's information at will.

#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
using namespace std;

void calcAverage();    // Prototype for the function that will average all the test scores.

struct StudentInfo
{
string Name;    // Student's name.
string IDNum;    // Student's ID number.
double *Tests;    // Pointer to an array of test scores.
double Average;    // Average test score.
};

// The main function will allow the user to set up a list of students, track the test scores of each student individually, average the test scores of each student individually, and will then display all student information.
int main()
{
StudentInfo *Students;    // This will be the array to hold all of the students.
double *Tests;    // This will be a 2-dimensional array that holds the test scores for each student.
string choice1;    // Temporary home for user input so that we don't loop endlessly when a character is entered in place of a number.
int numStudents;    // The number of students we will process.
int numTests;    // The number of tests being processed per student.

// Get the number of students to process.
cout << "\nHow many students will we be dealing with today? ->    ";
getline(cin, choice1);
stringstream(choice1) >> numStudents;

// If the input is invalid, loop the question until the user gets it right.
while (numStudents < 1)
{
cout << "\nPlease enter a valid positive number ->    ";
getline(cin, choice1);
stringstream(choice1) >> numStudents;
}

Students = new StudentInfo[numStudents];    // This dynamically allocates an array of 'StudentInfo' structures.

// Get the number of tests to process per student.
cout << "\nHow many tests are we handling for this semester? ->    ";
getline(cin, choice1);
stringstream(choice1) >> numTests;

// If the input is invalid, loop the question until the user gets it right.
while (numTests < 1)
{
cout << "\nPlease enter a valid positive number ->    ";
getline(cin, choice1);
stringstream(choice1) >> numTests;
}

Tests = new double[numStudents][numTests];``````

And the error I get is:

``````pkCourseGradifier1.cpp: In function ‘int main()’:
pkCourseGradifier1.cpp:60: error: ‘numTests’ cannot appear in a constant-expression``````

Why does it do this?

I'm assuming it's trying to treat 'numTests' as a constant, but then why doesn't it also do this for the 'numStudents'?

[EDIT] Ok, so I figured out that c++ makes you use constant expression for every dimension after the first when dynamically allocating space for an array. Is there any way to get the value of 'numTests' and put it into a constant expression? [/EDIT]

You're going about making the 2D array the wrong way. You have to separately create each 1D element of it:

[CODE] Tests = new double*[numStudents];
for (int i = 0; i < numStudents; ++i)
Tests[i] = new double[numTests];[/CODE]

## All 4 Replies

You're going about making the 2D array the wrong way. You have to separately create each 1D element of it:

``````Tests = new double*[numStudents];
for (int i = 0; i < numStudents; ++i)
Tests[i] = new double[numTests];``````

Thank you kindly, sir!

That takes care of that, and the rest is a breeze. *whew*

P.S. *for my fellow noobs: dont't forget to add another asterisk in front of the variable at line 27*

To be thorough, you should also test for exceptions--in case memory can not be allocated.

I have posted three versions of a program that dynamically allocates memory for a 2D matrix on my website (see my sig, near the bottom of the page: "Dynamic Arrays in C++").