0

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;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Who or what text suggest `fflush(stdin);` and `gets(stud.name);`? – chux - Reinstate Monica Apr 17 '22 at 22:43
  • 3
    --> [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input) – chux - Reinstate Monica Apr 17 '22 at 22:45
  • 2
    There is no need to tell us the whole journey of how you got to this code. It just makes the question difficult to understand. What exactly is your one specific question? Is it "how to remove the new line "fgets" adds". – kaylum Apr 17 '22 at 22:45
  • 2
    Note the questions [Why `gets()` is too dangerous to be used — ever!](https://stackoverflow.com/q/1694036/15168) for reasons why you should never use `gets()`, and [Using `fflush(stdin)`](https://stackoverflow.com/q/2979209/15168) for reasons why that is generally a dubious option. – Jonathan Leffler Apr 17 '22 at 22:48
  • And your problem is probably [`scanf()` leaves the newline character in the input buffer](https://stackoverflow.com/q/5240789/15168). – Jonathan Leffler Apr 17 '22 at 22:51
  • Thanks for the help! it really helps allot. I'll also take your advice about posting a question @kaylum, you are right is confusing what I want to know. – Kuudere-san Apr 18 '22 at 01:08
  • @chux-ReinstateMonica It wasn't any text, the teacher just put us this code on the whiteboard while explaining how to interact with structs. – Kuudere-san Apr 18 '22 at 01:11
  • 1
    @Kuudere-san Note that `fflush(stdin); gets(stud.name);` is at least 20 years out-moded in teaching C. You have my sympathy. – chux - Reinstate Monica Apr 18 '22 at 02:47

0 Answers0