c - 尽管 undefined variable 但不增加

标签 c variables increment post-increment

正如你们所看到的,我没有定义变量“n_students”,因此它应该能够毫无问题地递增,但事实并非如此。当我将其放入用户定义函数中时,它也不会增加,那么这里的问题是什么?

#define MAX_CLASS_SIZE 5
#define MAX_NAME_SIZE 11

    int main(void){
        student_t studentlist[MAX_NAME_SIZE];
        int n_students = 0; 
        int result;
        /* TODO */

        while (1){
        printMenu();
        int choice;
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                if (n_students == MAX_CLASS_SIZE) {
                    printf("Class is full\n");
                }
                else {
                    addStudent(studentlist, n_students);
                    n_students++;
                }
  case 2:
            if (n_students == 0) {
                    printf("Class is empty\n");
                } else {
                    n_students--;
                }
                break;
        case 3:
            if (n_students == 0){
                    printf("Class is empty\n");
                } 
                else {
                    displayStudents(studentlist, n_students);
                } 
                break;
        case 4:
            saveDatabase(studentlist, n_students);
                break;
        case 5:
            result = loadDatabase(studentlist);
            if (result >= 0){
                n_students = result;
            }
            break;
        case 6:
            return 0;
        default:
            printf("Invalid choice.\n");
            break;
            }
    }

}


void addStudent(student_t *studentlist,int n_students)
{
    char name[1024];
    printf("Enter name>");
    scanf("%s", name);
    name[MAX_NAME_SIZE-1]= '\0';
    strcpy(studentlist[n_students].name, name);

    int day,month,year;
    while (1){
        printf("Enter birthday: day>");
        scanf("%d", &day);
        if (day >= 1 && day <=31){
            break;
        }
        printf("Invalid day. ");
    }

    while (1){
        printf("Enter birthday: month>");
        scanf("%d", &month);
        if(month >= 1 && month <= 12){
            break;
        }
        printf("Invalid month. ");
    }

    while (1){
        printf("Enter birthday: year>");
        scanf("%d", &year);
        if (year >= 1800 && year <= 2017){
            break;
        }
        printf("Invalid year. ");
    }

    float gpa;
    while (1){
        printf("Enter GPA>");
        scanf("%f", &gpa);
        if (gpa >= 0.0 && gpa <= 4){
            break;
        }
        printf("Invalid GPA. ");
    }

    studentlist[n_students].birthday.day = day;
    studentlist[n_students].birthday.month = month;
    studentlist[n_students].birthday.year = year;
    studentlist[n_students].gpa = gpa;
}

最佳答案

首先,您在情况1之后错过了休息

    switch (choice)
    {
        case 1:
            if (n_students == MAX_CLASS_SIZE)
            {
                printf("Class is full\n");
            }
            else
            {
                addStudent(studentlist, n_students);
                n_students++;
            }
            break; <----------------------
        case 2:

否则执行情况2并执行n_students--;

此外,您的代码正在检查数组索引 n_stundent

if (n_students == MAX_CLASS_SIZE)

所以数组大小应该是

student_t studentlist[MAX_CLASS_SIZE];

关于c - 尽管 undefined variable 但不增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43799733/

相关文章:

C段故障 Raspberry Pi2

swift - 如何使用与变量名称匹配的字符串访问变量并为其赋值

css - 我如何包含一个包含变量的混合,然后在父组合器中更改该变量值?

ruby - 在 Ruby 中增加字符串的一部分

c++ - 通过指针解引用并递增给出一个不同的值,然后递减它,取而代之的是C++

c - 在 C 编程中使用 for 循环打印特定模式

c - 如何通过C编程实现XMPP上的ssh?

mysql - ErrCode 为 "Select Into Outfile with a variable"- 权限困惑

javascript - 这个函数如何递增?

c - 尝试打印 char 数组中特定索引中的 char