Im having trouble with the syntax here. We are studying structures and pointers in class currently and are tasked with creating a dynamic array of a single structure with a pointer array inside to both be allocated and deleted by the end of the program. (Hopefully that made sense)
Here are the snippits of code im working with, note how the entry of scores works:
std::cin << stuArray[i].stuScore[j]
But then the deletion in a similar manner, does not:
delete[] stuArray[count].stuScore[j];
Deletion Code:
do
{
for (unsigned short j = 0; j < numTests; j++)
{
delete[] stuArray[count].stuScore[0]; //Syntax???????
}
count++;
} while (count < numStudents);
delete[] stuArray;
Score Entry Code (Which Works)
bool ScoreEntry(Student * stuArray, unsigned short numStudents, unsigned short numTests)
{
//Local Variables
unsigned short idTempChoice = 0;
//Get Id Number
std::cout << "\nChoose a student by ID and enter the test scores: ";
std::cin >> idTempChoice;
//Id lookup
for (unsigned short i = 0; i < numStudents; i++)
{
//Id Check
if (idTempChoice == stuArray[i].stuId)
{
std::cout << "Student selected: " << stuArray[i].stuName << std::endl;
//Score Entry
for (unsigned short j = 0; j < numTests; j++)
{
std::cout << "Test " << j + 1 << "'s Score: ";
std::cin >> stuArray[i].stuScore[j];
}//End For Loop j
return true;
}
}//End For Loop i
//Student Id not found
std::cout << "Student not found!\n";
return false;
}
Allocation Code (Struct):
void MemAllocation(Student * &stuArray, unsigned short &numStudents)
{
//Get Number of students
std::cout << "How many students have taken the test: ";
std::cin >> numStudents;
std::cout << std::endl;
//Dynamically allocate pointers
stuArray = new Student[numStudents];
}
Allocation Code (Pointer inside struct):
for (unsigned short i = 0; i < numTests; i++) //Allocate Dynamic array for each student
{
stuArray[i].stuScore = new float[numTests];
}
This is Literally all the code you need reference, this is not a bug its a syntax problem :)
stuArray[count].stuScorewithnew[], but notstuArray[count].stuScore[j]. Onlydelete[]what you actuallynew[].delete[] stuArray[count].stuScorebut notdelete[] stuArray[count].stuScore[j]