1

So I'm trying to write a program in which I need to enter informations like name, surname, student id, birthday for multiple students . The thing is I can't get it to print the info for all the students. This version of the code I wrote just prints the variables without anything stored in them or with some weird characters. In an earlier version of the script the info I typed in would just overwrite the previous info and it would print just one student's info. I think I need to make some changes in the for loop if I'm not mistaken. If someone could give me a hand, I'd appreciate it.

Here's the code:

#include <stdio.h>
#include <string.h>
#define students 200



typedef struct {
    char name[20];
    char surname[20];
    int studentid[5];
    int day[5];
    int month[5];
    int year[5];
}student;

int main(){ 
    student a[students];
    int j;
    int n;
    int i;  
    int choice;
    for(i=0;i<=students;i++){           
        printf("\n===========================================================\n");
        printf("\n1 Enter info for a student");
        printf("\n2 Print all the students");
        printf("\n3 End\n");
        printf("\n===========================================================\n");
        printf("\nChoose something ---> ");
        scanf("%d", &choice);
        switch(choice)
        {
            case 1:
                printf("Enter name: \n");
                scanf("%s", a[students].name);
                printf("Enter surname: \n");
                scanf("%s", a[students].surname);
                printf("Enter student ID: \n");
                scanf("%s", a[students].studentid); 
                printf("Enter day: \n");
                scanf("%s", a[students].day);
                printf("Enter month: \n");
                scanf("%s", a[students].month);
                printf("Enter year: \n");
                scanf("%s", a[students].year);                                      
                break;
            case 2:
                for(i=0;i<students;i++)
                {
                    printf("\nNome student: %s\nSurname student: %s\nStudent id: %s\nStudent Birthday: %s.%s.%s\n", a[i].name, a[i].surname, a[i].studentid, a[i].day, a[i].month, a[i].year);
                }
                break;
            case 3:
                break;
            default:
                printf("Choose again!\n");

        }

    } 
    return 0;
}

Thanks!

1
  • It's good practice to use all upper-case names for preprocessor-defined variables. So, #define STUDENTS 20. Commented May 20, 2020 at 20:13

2 Answers 2

2
  1. Did you see the warning when you compile:

warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat=]

For string, you should use the character array, so the parameters of the struct student should change to:

    char studentid[10];
    char day[10]; // 5 is too short, for example monday need at lest 7 characters (1 for null character at the end of string)
    char month[10];
    char year[10];
  1. You declare the array with length = students but you try to access the (students+1)th (a[students])

The for loop should change to:

for(i=0;i < students;i++){} // i from 0 to students-1 not to students.

You should use the counter (you can declare the variable count for example) to count the number of students that you enter the info. It will be useful when you print the info of these students.

                printf("Enter name: \n");
                scanf(" %19s", a[count].name);
                printf("Enter surname: \n");
                scanf(" %19s", a[count].surname);
                printf("Enter student ID: \n");
                scanf(" %9s", a[count].studentid); 
                printf("Enter day: \n");
                scanf(" %9s", a[count].day);
                printf("Enter month: \n");
                scanf(" %9s", a[count].month);
                printf("Enter year: \n");
                scanf(" %9s", a[count].year);
                count++; // increase count after each student
                break;

case 2 changes to:

                for(int j=0;j<count;j++) // just print the students that you set the info in case 1.
                {
                    printf("\nNome student: %s\nSurname student: %s\nStudent id: %s\nStudent Birthday: %s.%s.%s\n", a[j].name, a[j].surname, a[j].studentid, a[j].day, a[j].month, a[j].year);
                }
                break;

This line: #define students 200 is not wrong, but you should use the difference name and use the uppercase, it's easier to understand the constant value, for example:

#define MAX_NUM_STUDENTS 200

The complete code:

#include <stdio.h>
#include <string.h>
#define MAX_NUM_STUDENTS 200

typedef struct {
    char name[20];
    char surname[20];
    char studentid[10];
    char day[10];
    char month[10];
    char year[10];
}student;

int main(){ 
    student a[MAX_NUM_STUDENTS];
    int i, n, choice, count = 0;
    for(i=0;i<MAX_NUM_STUDENTS;i++){           
        printf("\n===========================================================\n");
        printf("\n1 Enter info for a student");
        printf("\n2 Print all the students");
        printf("\n3 End\n");
        printf("\n===========================================================\n");
        printf("\nChoose something ---> ");
        scanf("%d", &choice);

        switch(choice)
        {
            case 1:
                printf("Enter name: \n");
                scanf(" %19s", a[count].name);
                printf("Enter surname: \n");
                scanf(" %19s", a[count].surname);
                printf("Enter student ID: \n");
                scanf(" %9s", a[count].studentid); 
                printf("Enter day: \n");
                scanf(" %9s", a[count].day);
                printf("Enter month: \n");
                scanf(" %9s", a[count].month);
                printf("Enter year: \n");
                scanf(" %9s", a[count].year);   
                count++;
                break;
            case 2:
                for(int j=0;j<count;j++) // just print the students that you set the info in case 1.
                {
                    printf("\nNome student: %s\nSurname student: %s\nStudent id: %s\nStudent Birthday: %s.%s.%s\n", a[j].name, a[j].surname, a[j].studentid, a[j].day, a[j].month, a[j].year);
                }
                break;
            case 3:
                break;
            default:
                printf("Choose again!\n");
        }

    } 
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the comment. It works but I've got one small problem when trying to print the info for the students I typed in. The program prints out empty variables from the struct (name, surname etc) multiple times besides the ones I've already used. Is there any way to prevent that and print only used variables?
@Zenith sorry, it's typo for(int j=0;i<count;j++) change to for(int j=0;j<count;j++). I edited the code.
0

You're storing the user input at a[students]. However, the array a (by the way, you should be descriptive with your variable names) is only students-elements long. Therefore, the students-th element is past the end of the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.