0

Hi I have to create a database that stores students number, name and also stores an array of course marks (1-N) in C Programming Language.

Everything worked until I started coding for the array of course marks. Then every time I compiled the code it kept crashing as soon as it asked to input the course marks.

Can you please tell me where I'm going wrong in my programming for this task? I have attached it to this message.

The program worked for inputting the name, student number, however I could not get the program to input array of marks. I have asked how many course marks to be entered and then used a for loop within the "void insert(void)" function to keep inputting the course marks into the array *marks. I am referring specifically to lines 24 to 30 in my programming code.

Always at this point the program kept crashing and I could not proceed further to enter more names or print the stored student details.

I think there is a problem with this part:

for (i= 0; i < num_marks; i++)
{
    printf("Enter Course Mark: \n");
    scanf("%d", &(list[num_students].marks[num_marks]));
}

Anyway here is the full code:

#include <stdio.h>
#include <string.h>

struct student{
    int number;
    char name[10];
    int marks[5];
};
struct student list[10];

int num_students = 0;
int num_marks = 0;
int *p;

void insert(void)
{
    int student_number;
    int i;
    printf("Enter number: \n");
    scanf("%d", &list[num_students].number);
    printf("Enter NAME: \n");
    scanf("%s", &list[num_students].name);
    printf("Enter NO of courses: \n");
    scanf("%d", num_marks);
    for (i= 0; i < num_marks; i++)
    {
        printf("Enter Course Mark: \n" );
        scanf("%d", &(list[num_students].marks[num_marks]));
    }
    num_students++; // HOW DO WE INPUT ARRAY MARKS??? MARK1: , MARK2: , MARK3 ,
}

void printtest(void)
{
    int i;
    for (i=0; i < num_students; i++)
    {
        printf("Name: \n");
        puts(list[i].name);
        printf("Number: %d \n", list[i].number);
        printf("Mark: %d /100 \n", list[i].marks);
        printf("\n");
    }
}

int main(void)
{
    int code;
    int opt1;
    int courses, i, k, j, counter;
    for (;;){
        printf("Enter operation code: \n");
        printf("(1) ADD NEW STUDENT DETAILS: \n");
        printf("(2) DISPLAY REPORT OF ALL STUDENTS: \n");
        scanf(" %d", &code);
        switch (code){
            case 1 :
                insert();
                break;
            case 2 :
                printtest();
                break;
            default:
                printf("Illegal code\n");
                printf("\n");
        }
    }
}

2 Answers 2

1

Apart from what others pointed out, I'd like to draw your attention towards the following:

void insert(void)
    {
        int student_number;
        int i;
        printf("Enter number: \n");
        scanf("%d", &list[num_students].number);
        printf("Enter NAME: \n");
        scanf("%s", &list[num_students].name);
        printf("Enter NO of courses: \n");
        scanf("%d", num_marks);
        for (i= 0; i < num_marks; i++)
        {
            printf("Enter Course Mark: \n" );
            scanf("%d", &(list[num_students].marks[num_marks])); 
        }
        num_students++; 
    }

    void printtest(void)
    {
        int i;
        for (i=0; i < num_students; i++)
        {
        printf("Name: \n");
        puts(list[i].name);
            printf("Number: %d \n", list[i].number);
            printf("Mark: %d /100 \n", list[i].marks);
        printf("\n");
        }
    }

There are three problematic statements:

  1. scanf("%s", &list[num_students].name); This is why beginners should use compilers with all warnings enabled.
  2. printf("Mark: %d /100 \n", list[i].marks); What did you declare marks as in the first place?
  3. scanf("%d", num_marks); Seems like you put & operator where not needed and ignore where needed. Read your textbook before asking a question next time.

Seems like you're having a tough time understanding the concept of arrays and pointers. Read your text book thoroughly before venturing into the world of pointers. If you don't use them correctly, even compiler can't help you.

Also, even if I don't expect your program to have a robust input mechanism, at least array bounds checking is expected. Learn good habits from the beginning. They'll save a lot of your time while debugging later.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for advice. Can you still tell me where I am going wrong in the insert function. I am more concerned why I cant enter the student marks.
yes I have however it only works now because it increments with fixed no (ie 4) not like before user defined num_marks
for (i= 0; i < num_marks; i++) changed to for (i= 0; i < 4 i++)
Did you enter a number greater than 5 in num_marks?
no even when I entered 2 in num_marks the program crashed
|
1

Seems to be an error in:

for (i= 0; i < num_marks; i++)
{
    printf("Enter Course Mark: \n" );
    scanf("%d", &(list[num_students].marks[num_marks]));
}

The crash is probably because num_marks as index indexes beyond the array. Change to:

for (i= 0; i < num_marks; i++)
{
    printf("Enter Course Mark: \n" );
    scanf("%d", &(list[num_students].marks[i]));
}

2 Comments

Thanks for your help ... However even after I changed &(list[num_students].marks[num_marks])) into &(list[num_students].marks[i])) the program still crashed. Do you know what else could be wrong?
See also @Mayank-Verma's answer to solve more problems.

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.