multiple input arrays

I have built this program and for some reason am having memory issues remembering how to make is so I can input more than one lab and exam score. To be more specific on how to input 3 exams and 5 labs for each student. Any help would be great. I know its something simple, but its been so long since my last class.


#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

//setting limit to number of students
const int MAX_STU = 24;

//declaring functions
int getStuCount();
int getStuName(int num, string name[]);
int getExamScores(int num, int exam[]);
int getLabScores(int num, double lab[]);
int calculatePointGrades(int exam[], double lab[], int num, double points[]);
int calculateLetterGrades(double points[], int num, char letter[]);
int showGradeData(int exam[], double lab[], double points[], char letter[], int num, string name[]);
double intArrayAve(int dataArr[], int num);
double doubleArrayAve(double dataArr[], int num);


int main(int argc, char *argv[])
{
//declaring arrays
string name[MAX_STU];
int exam[MAX_STU];
double lab[MAX_STU];
double points[MAX_STU];
char letter[MAX_STU];

int num = getStuCount();

getStuName(num, name);
getExamScores(num, exam);
getLabScores(num, lab);
calculatePointGrades(exam, lab, num, points);
calculateLetterGrades(points, num, letter);

// display the result
showGradeData(exam, lab, points, letter, num, name);

system("PAUSE");
return EXIT_SUCCESS;
}

//getting number of students
int getStuCount()
{
int num = 0;
cout << "Enter the number of students: ";
cin >> num;

return num;
}

//getting names of students
int getStuName(int num, string name[])
{

for(int i = 0; i < num; i++)
{
cout << "Enter the name of student" << (i + 1) << ":" << endl;
cin >> name[i];
}
return 0;
}

//get the exam scores for the students
int getExamScores(int num, int exam[])
{
for (int i = 0; i < num; i++)
{
do
{
cout << "Enter the exam score of student " << (i + 1) << "(0-100): ";
cin >> exam[i];
} while (exam[i] < 0 || exam[i] > 100); // check range 0-100
}
return 0;
}

//get the lab scores for the students
int getLabScores(int num, double lab[])
{
for (int i = 0; i < num; i++)
{
do
{
// read score
cout << "Enter the lab score of student " << (i + 1) << "(0-100.0): ";
cin >> lab[i];
} while (lab[i] < 0 || lab[i] > 100); // check range 0-100.0
}
return 0;
}

// Calculate point grades and store in the array.
int calculatePointGrades(int exam[], double lab[], int num, double points[])
{
for (int i = 0; i < num; i++)
{
points[i] = exam[i] * 0.5 + lab[i] * 0.5;
}
return 0;
}

// Calculate letter grades and store in the array.
int calculateLetterGrades(double points[], int num, char letter[])
{
for (int i = 0; i < num; i++)
{
if (points[i] >= 90)
letter[i] = 'A';
else if (points[i] >= 80)
letter[i] = 'B';
else if (points[i] >= 70)
letter[i] = 'C';
else if (points[i] >= 60)
letter[i] = 'D';
else
letter[i] = 'F';
}
return 0;
}

//Display resluts
int showGradeData(int exam[], double lab[], double points[], char letter[], int num, string name[])
{

//cout.precision(1);
for (int i = 0; i < num; i++)
{
cout << name[i] << "\t" << exam[i] << "\t" << doubleArrayAve(lab, num) << "\t" << doubleArrayAve(points, num) << letter[i] << endl;
/*cout << exam[i] << endl;
cout << lab[i] << endl;
cout << points[i] << endl;
cout << letter[i] << endl;
cout << endl;
*/
}

// display average
cout << endl;
cout << "Exam Average: " << intArrayAve(exam, num) << endl;
cout << "Lab Average Average: " << doubleArrayAve(lab, num) << endl;
cout << "Point Grade Average: " << doubleArrayAve(points, num) << endl;

return 0;
}

// Calculate and return average of the values in an int array
double intArrayAve(int dataArr[], int num)
{
if (num == 0)
return 0;
// calculate the sum
double total = 0;
for (int i = 0; i < num; i++)
total += dataArr[i];
// return the average
return total / num;
}

// Calculate and return average of the values in an double array
double doubleArrayAve(double dataArr[], int num)
{
if (num == 0)
return 0;
// calculate the sum
double total = 0;
for (int i = 0; i < num; i++)
total += dataArr[i];
// return the average
return total / num;
}




no one?
On the surface I don't see the problem with your code but since it is pasted in there without the code and /code in brackets for easier reading, I can't be sure. Instead I will tell you how to find the problem, it is a trick I use whenever I have a memory issue. When the program crashes, it usually tells you where is crashes at. Place a breakpoint (MSVC++ is F9 for that). Then you can press F10 or F11 to run each line of code step by step. Since your code is small, you can actually just press F10 to run it from the beginning step by step.

F10 will just run the line of code where as F11 if there is a function, it will step into the function. This is good to know because if a function works they way it is suppose to work, then you can press F10 to speed things up or F11 if it is the problem, returning something it shouldn't.

While pressing F10 or F11, you can watch the variables and see if they are what they are suppose to be at any given time. once something changes in a way from your plan of the program, you know where your problem lies.

My guess is somewhere num is greater then your max number of students. In fact there should be an error check to make sure that the number of students doesn't exceed your max student count.
I'm not getting any errors, I am just forgetting how to use the array to build and store more data for each student. It is only storing the one exam and lab for both, but I need 3 labs and 5 exams for each student.
Topic archived. No new replies allowed.