Deonte Threatt Deonte Threatt - 25 days ago 9
C Question

Gradebook, array not saving

So I'm trying to make a grade book that does everything displayMenu() says. But i cant even get the student ID to save when i go to view the grades. Please Help.

Everything is initialized here

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("pause")
#define CLS system("cls")
#define EXAMS 100
#define STUDENT 4
void displayArray(float grades[STUDENT][EXAMS]);
void newStudent(float grades[STUDENT][EXAMS]);
void displayStudentAverage(float grades[STUDENT][EXAMS]);

int main() {
float grades[STUDENT][EXAMS];


This is everything the program should do

displayMenu(grades, 0 );

} // end of main;
int displayMenu(float grades[STUDENT][EXAMS]) {
printf("\t \t MENU \t \t");
printf("Enter Corresponding Number\n");
printf("1.Enter New Student\n");
printf("2.Change Existing Grades\n");
printf("3.View All Grades\n");
printf("4.View Average Score Per Student\n");
printf("5.View Average Score Per Exam\n");
printf("6.View Average Score For The class\n");
printf("7.CLEAR GRADEBOOK\n");
printf("8. Save Gradebook\n");
printf("8.Exit\n");
int choice = 0;
scanf("%d", &choice);
switch (choice) {
case 1:
newStudent(grades, 0);
CLS;
displayMenu(grades,0);
break;
case 2:
break;
case 3: displayArray(grades, 0);
CLS;
displayMenu(grades,0);
break;
case 4:
displayStudentAverage(grades, 0);
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
exit(0);
break;
case 9:
exit(0);
break;
default: printf("You Have entered an incorrect number");
PAUSE;
}
}


This is what displays the grades

void displayArray(float grades[STUDENT][EXAMS]) {

printf("%.1f\t", grades[STUDENT][EXAMS]);

}


I'm trying to add the values to the array here

void newStudent(float grades[STUDENT][EXAMS]) {
float addgrade;
printf("Please Enter Student ID: ");
scanf("%f", &grades[STUDENT][EXAMS]);
printf("Enter four exam grades, use comma to split grades");

scanf("%f", addgrade);
grades[STUDENT][EXAMS] += addgrade;

PAUSE;
CLS;



}

void displayStudentAverage(float grades[STUDENT][EXAMS]) {
int sum, loop;
float avg;

sum = avg = 0;

for (loop = 0; loop < 10; loop++) {
sum = sum + grades[loop];
}

avg = (float)sum / loop;

printf("Average of array values is %.2f", avg);

}

Answer Source

First of all, it's always good check the compiler warnings to get some hints to possible bugs...

Here's a list of problems in the code:

  • no header files included
  • displayMenu prototype is missing
  • the grades array is used with inconsistent types (float/int)
  • with grades[STUDENT][EXAMS] the grades array is accessed out of bounds (for example if you define an array of size 5 you can only access position 0 to 4)
  • the return type of main needs to be int
  • the function newStudent has return type void but the code tries to return something with return &grades[STUDENT][EXAMS];

Apart from that, the code should work...