'I'm new to C and am stuck with many issues, I have fixed them but I wonder if there are better ways

I'm new to C just coming out of my second university class that explained structs and string functions. I tried running the code we had written in the lesson and ban, one segmentation error, "gets" suddenly is banned by my compiler and I have spent the last couple of hours trying to make it work with rubber band solutions of bread-crumbs I found online.

As luck would have it I made it work, yet I would like to learn more: better solutions, other options, etc. I really would like a little guidance even if just to resources.

It's a simple code that ask for an int, then a string and a float, and loops around until the int is 0.

A problem that I couldn't fix is how to remove the new line "fgets" adds to get the style that I put the output in class, I don't know if I ask Google the wrong answer or if it isn't ask much because it is to obvious.

And before the recommendations of leaving the university come up, I'll ask the teacher about this, and hopefully he will give a good reason why he taught us like this.

Class code:

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

#define CLEAR system("cls||clear")

typedef struct a
{
    int id;
    char *name;
    float avarage;
} student;

int main()
{
    student stud;

    printf("\n Input ID: ");
    scanf("%d", &stud.id);

    while (stud.id != 0)
    {
        fflush(stdin);

        printf("\n input the name: ");
        gets(stud.name);

        fflush(stdin);

        printf("\n input the avarage: ");
        scanf("%f", &stud.avarage);

        CLEAR;

        printf ("\n ID: %d | Name: %s | Avarage: %.2f", stud.id, stud.name, stud.avarage);

        printf("\n\n Input ID: ");
        scanf("%d" , &stud.id);
    }
    CLEAR;

    return 0;
}

First I found fgets I put it in and it struck me with a segmentation error, Google what that was, then I used malloc to allocate memory space to the stud.name pointer, then it didn't blow up but i couldn't type anything in stud.name, spent 1 hour searching why, until I discover that it is because of the input buffer that is storing a new line from the first scanf (and the last by proxy) and that is what the fgets is reading, tried fflush(stdin): doesn't work, and then I find that getchar() stores this new line and it let me finally write in stud.name. Then, it finally worked.

My code:

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

#define CLEAR system("cls||clear")

typedef struct a
{
    int id;
    char *name;
    float avarage;
} student;

int main()
{
    student stud;

    stud.name = (char *) malloc(sizeof(char) * 30);

    printf("\n Input ID: ");
    scanf("%d", &stud.id);
    getchar();

    while (stud.id != 0)
    {
        printf("\n input the name: ");
        fgets(stud.name, 30, stdin);

        printf("\n input the avarage: ");
        scanf("%f", &stud.avarage);
        getchar();

        CLEAR;

        printf ("\n ID: %d | Name: %s | Avarage: %.2f", stud.id, stud.name, stud.avarage);

        printf("\n\n Input ID: ");
        scanf("%d" , &stud.id);
        getchar();
    }
    CLEAR;

    return 0;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source