成绩簿,数组不保存

时间:2017-09-24 21:20:22

标签: c

所以我正在努力制作一本可以完成所有displayMenu()所说的成绩簿。但是当我去查看成绩时,我甚至无法获得学生证。请帮助。

一切都在这里初始化

#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];

这是程序应该做的一切

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;
}
}

这是显示成绩的原因

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

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

}

我正在尝试将值添加到此处的数组

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);

}

1 个答案:

答案 0 :(得分:1)

首先,检查编译器警告总是很好,以获得一些可能的错误提示......

以下是代码中的问题列表:

  • 不包含头文件
  • displayMenu原型缺失
  • grades数组用于不一致的类型(float / int
  • grades[STUDENT][EXAMS]访问grades数组超出范围(例如,如果定义大小为5的数组,则只能访问位置0到4)
  • main的返回类型需要为int
  • 函数newStudent的返回类型为void,但代码尝试返回return &grades[STUDENT][EXAMS];
  • 的内容

除此之外,代码应该有效......