Home > Enterprise >  storing 10 students information in a struct ,debug assertion failed error
storing 10 students information in a struct ,debug assertion failed error

Time:06-01

I wrote this but it keeps giving a debug assertion failed error after entering age.

I tried everything I knew and checked with online examples too but couldn't figure out what's wrong.

#include <stdlib.h>
#include <stdio.h>

struct student
{
    char name[50];
    float age;
    float grade[2];
    float gpa;

};

int main() {
    struct student s[10];
    int i;

    for (i = 0; i < 10; i  ) {
        printf("please enter the %d student's name\n", i   1);
        scanf_s(" %s ", &s[i].name, 20);
        printf("please enter the student's age\n");
        scanf_s("%f", &s[i].age);
        printf("please enter the physics score\n");
        scanf_s("%.2f", &s[i].grade[0]);
        printf("please enter the maths score\n");
        scanf_s("%.2f", &s[i].grade[1]);
        s[i].gpa = (s[i].grade[0]   s[i].grade[1]) / 2;
    }
    printf(" name\t\t age\t physics\tmaths\tgpa\n");
    printf("_____________________________________________________________________");
    for (i = 0; i < 10; i  ) {
        printf("%s\t %f\t %.2f\t%.2f\t%.2f\n", s[i].name, s[i].age, s[i].grade[0], s[i].grade[1], s[i].gpa);
    }
    return 0;
}

CodePudding user response:

I assume you're using Microsoft Visual Studio. You need to tell us such details.

You're misusing scanf_s and the compiler warned you about this. These warnings should be considered as errors.

Forget scanf_s and use scanf instead. To be able to do so, you need to put this at the very top of your source code:

#define _CRT_SECURE_NO_WARNINGS

Furthermore replace "%.2f" with "%f" and remember that scanf is not printf, some of the format specifiers as %s and %d are the same, whereas others are not. Also replace scanf(" %s ", &s[i].name); with scanf("%s", s[i].name);

Be aware that you cannot enter a name containing spaces with scanf and %s, use "%[^\n]" instead of %s (but that's an advanced topic), don't bother for the moment.

Anyway scanf is absolutely not suited for interative user input in real world programs, for small test programs it's OK.

CodePudding user response:

#include <stdlib.h>
#include <stdio.h>

struct student
{
    char name[50];
    float age;
    float grade[2];
    float gpa;

};

int main() {
    struct student s[10];
    int i;

    for (i = 0; i < 10; i  ) {
        printf("please enter the %d student's name\n", i   1);
        scanf("%s", &s[i].name[0]);
        printf("please enter the student's age\n");
        scanf("%f", &s[i].age);
        printf("please enter the physics score\n");
        scanf("%f", &s[i].grade[0]);
        printf("please enter the maths score\n");
        scanf("%f", &s[i].grade[1]);
        s[i].gpa = (s[i].grade[0]   s[i].grade[1]) / 2;
    }
    printf(" name\t\t age\t physics\tmaths\tgpa\n");
    printf("_____________________________________________________________________");
    for (i = 0; i < 10; i  ) {
        printf("%s\t %f\t %.2f\t%.2f\t%.2f\n", s[i].name, s[i].age, s[i].grade[0], s[i].grade[1], s[i].gpa);
    }
    return 0;
}

The code i just shared here is working how i believe you want it to. Changes i made: Used scanf instead of scanf_s; Retired the spaces between the quotation marks on the first scanf(); Added [0] after &s[i].name in order to provide the scanf() function with a single address; Also replaced %.2f with %f, the function scanf() doesn't allow it;

  • Related