-1

As you guys see, I did not define the variable "n_students" therefore it should be able to increment without any problem, but it does not. It also does not increment when I put it inside the user define function, so what seems to be the problem here?

#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;
}
LPs
  • 16,045
  • 8
  • 30
  • 61
Anh Minh Tran
  • 341
  • 1
  • 3
  • 11
  • How do You know it does not increment? What does the program do? Are You sure, that the `switch` goes through the `case 1`? – Roman Hocke May 05 '17 at 08:10
  • Is MAX_CLASS_SIZE defined and not equal to 0, and are you sure you are entering your '1' case there? – Izuka May 05 '17 at 08:10
  • Post the full code or, at least, all variables/macros involved – LPs May 05 '17 at 08:11
  • 4
    There isn't enough information here to be able to answer. Please work on an MCVE ([MCVE]) so that we can see the real problem. As it stands, `n_students` should be incremented when you add a student unless `MAX_CLASS_SIZE` is zero. – Jonathan Leffler May 05 '17 at 08:11
  • Turn on all your compiler warnings and mind them. Your immediate problem is not reflected in the code you posted. – pmg May 05 '17 at 08:12
  • The MAX_CLASS_SIZE is defined to be 11. I do not understand your second question though – Anh Minh Tran May 05 '17 at 08:12
  • Take also note that first time `addStudent` is called `n_student` is `0`.. – LPs May 05 '17 at 08:12
  • 1
    Notice you use two different macros: `MAX_NAME_SIZE` and `MAX_CLASS_SIZE`. – pmg May 05 '17 at 08:14
  • 5
    Missing `break;` means you're running into `n_students--;` afterwards. A debugger would've told you. – melpomene May 05 '17 at 08:16
  • 1
    `scanf("%s", name); name[MAX_NAME_SIZE-1]= '\0';` is also terrible – M.M May 05 '17 at 08:24
  • 1
    Please don't write any more code until you know how to use a debugger. TBH, you are already one app too late :( – ThingyWotsit May 05 '17 at 08:35

1 Answers1

2

First of all you missed a break after the case 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:

Otherwise the case 2 is executed and n_students--; performed.

Moreover your code is checking the array index n_stundent with

if (n_students == MAX_CLASS_SIZE)

so array size shiould be

student_t studentlist[MAX_CLASS_SIZE];
LPs
  • 16,045
  • 8
  • 30
  • 61