Home > Enterprise >  Making a school management system and getting errors
Making a school management system and getting errors

Time:05-01

I am new to programming and learning the C language and was practising structures and was trying to make basic school management system by using structures, but I am getting too many errors (by using pointers in structures). So, can you please tell me what are the errors and how can I avoid these types of mistakes. Because, according to my understanding this should have been working.

Here is my code:

#include <stdio.h>

struct student {
    int rollno;
    char name[20];
    float marks;
};

int main() {
    again:;

    while (1 == 1) {
        struct student array[60];
        char input;
        int roll;
        printf("What you would like to do:\n");
        printf("Enter 'R' to fill the details again of all the student\n Enter 'U' to edit the 
    details of a single student\n");
        printf("Enter 'S' to see the details of a particular student\n Enter 'A' to see the 
    details of every student\n");
        scanf("%c", &input);
        if (input == 'A' || 'a') {
            for (int i = 0; i < 60; i  ) {
                printf("Roll no. : %d\n Name : %s\n Marks : %f\n", array[i].rollno, array[i].name, 
     array[i].marks);
            }
        }
        else if (input == 'R' || 'r') {
            for (int i = 0; i < 60; i  ) {
                printf("Roll no. os student is : %d\n", (i   1));
                printf("Enter students name :\n");
                gets(array[i].name);
                printf("Enter students marks :\n");
                scanf("%f", &array[i].marks);
            }
        }
        else if (input == 'S' || 's') {
            printf("Enter the roll no. of the student of whom you want to see the details :\n");
            scanf("%d", &roll);
            printf("Roll no. :\n %d\n", roll);
            printf("Name : \n");
            puts(array[(roll - 1)].name);
            printf("Marks : \n %f \n", (*(array   (roll - 1))->marks));
        }
        else if (input == 'U' || 'u') {
            printf("Enter the roll no. of the student of whom you want to see the details :\n");
            scanf("%d", &roll);
            printf("Enter the name :\n");
            gets((*(array   (roll - 1))->name));
            printf("Enter the marks :\n");
            scanf("%f", &(*(array   (roll   1))->marks));
        } else {
            printf("Error Occured!!! \n");
            printf("Re-enter your input\n");
            goto again;
        }
    }
    return 0;
}

CodePudding user response:

There are many problems in your code:

  • there is no need for a goto again;, you already have an infinite loop while (1 == 1), which is usually written for (;;) in C. Don't use goto.

  • the definition struct student array[60]; should be moved outside the scope of the loop body. As posted, the array is discarded after each iteration and its contents become indeterminate.

  • array should be initialized to avoid undefined behavior when printing the contents before reading it from the user.

  • string literals cannot span multiple lines in a C source file. You can split them this way for readability:

      printf("What you would like to do:\n"
             "Enter 'R' to fill the details again of all the student\n"
             "Enter 'U' to edit the details of a single student\n"
             "Enter 'S' to see the details of a particular student\n"
             "Enter 'A' to see the details of every student\n");
    
  • you should have a menu option to quit the program

  • reading a single character with scanf("%c", &input) is tricky: scanf() will read the pending newline from a previous call. You should use scanf(" %c", &input) where the space will cause pending whitespace to be read and discarded.

  • if (input == 'A' || 'a') does not test for A or a, it compares input to 'A' and if different compares 'a' to zero, hence the test is always true. You should write:

      if (input == 'A' || input == 'a')
    
  • gets(array[i].name); is a NO NO. Don't use gets(), throw away the book that tells you to use it. For consistency with the other inputs, you can use

      scanf("[^\n]", array[i].name);
    

Note that 19 tells scanf() the maximum number of characters to store into array[i].name before the null terminator, preventing undefined behavior for longer user input, and [^\n] causes scanf() to read and store characters up to and not including the newline character. s would stop at any white space, preventing the input of multiple words such as James Bond.

  • scanf("%f", &(*(array (roll 1))->marks)); is a very contorted way to write:

      scanf("%f", &array[roll   1].marks);
    
  • you should check the return value of scanf() to detect invalid input and flush the pending input on error.

Here is a modified version:

#include <stdio.h>

struct student {
    int rollno;
    char name[20];
    float marks;
};

int main() {
    struct student array[60] = { 0 };
    int len = sizeof(array) / sizeof(*array);
    int c, i, roll;
    char input;

    for (;;) {
        printf("What you would like to do:\n"
               "Enter 'R' to fill the details of all the student\n"
               "Enter 'U' to edit the details of a single student\n"
               "Enter 'S' to see the details of a particular student\n"
               "Enter 'A' to see the details of every student\n"
               "Enter 'Q' to quit the program\n");
        if (scanf(" %c", &input) != 1)
            break;
        if (input == 'A' || input == 'a') {
            for (i = 0; i < len; i  ) {
                printf("Roll no.: %d\nName: %s\nMarks: %f\n",
                       array[i].rollno, array[i].name, array[i].marks);
            }
        } else
        if (input == 'R' || input == 'r') {
            for (i = 0; i < len; i  ) {
                printf("Roll no. os student is: %d\n", i   1);
                printf("Enter student's name:\n");
                if (scanf("[^\n]", array[i].name) != 1)
                    break;
                printf("Enter students marks:\n");
                if (scanf("%f", &array[i].marks) != 1)
                    break;
            }
            if (i < len) {
                printf("Invalid input\n");
            }
        } else
        if (input == 'S' || input == 's') {
            printf("Enter the roll no. of the student of whom you want to see the details:\n");
            if (scanf("%d", &roll) == 1 && roll >= 1 && roll <= len) {
                printf("Roll no.: %d\nName: %s\nMarks: %f\n",
                       roll, array[roll - 1].name, array[roll - 1].marks);
            } else {
                printf("Invalid Roll no\n");
            }
        } else
        if (input == 'U' || input == 'u') {
            printf("Enter the roll no. of the student of whom you want to see the details:\n");
            if (scanf("%d", &roll) == 1 && roll >= 1 && roll <= len) {
                printf("Enter the name:\n");
                scanf("[^\n]", array[roll - 1].name);
                printf("Enter the marks:\n");
                scanf("%f", &array[roll - 1].marks);
            } else {
                printf("Invalid Roll no\n");
            }
        } else
        if (input == 'Q' || input == 'q') {
            return 0;
        } else {
            printf("Invalid entry\n");
            printf("Re-enter your input\n");
        }
        /* read and discard the rest of the input line */
        while ((c = getchar()) != EOF && c != '\n')
            continue;
    }
    printf("Premature end of file\n");
    return 0;
}
  • Related