Search code examples
cgetsstring.h

Why isn't my program able to read a string with gets function and it passed throught it and gives me just the final results of the earlier scanfs


So this is a program where I want to input the asked variables from student 1 to student 2(school number, average grade, year of entrance and the alumns course), and I have to use a lib file, which I named alumn.h, where I have the typdef struct and the function copyAlumn which will copy from the input sturct ALUMN and return it's values to the second one. The thing is, in console everything works fine except reading it's String: when I use gets function it's will automatically pass it throught and give me the printed result. What's my error?

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

int main(){

    ALUMN A1,*A2;


    printf("Alumn A1:\nInsert the alumn number: ");
    scanf("%d",&A1.num);
    printf("Insert the year of entrance: ");
    scanf("%d",&A1.year);
    printf("Insert its avg. grade: ");
    scanf("%f",&A1.grade);
    printf("Insert its course: ");
    gets(A1.course);

    A2 = copyAlumn(&A1);

    if(A2 == NULL)
        return 0;

    printf("\n\nAlumn A2 = Copy from A1:\nNumber: %d\nYear: %d\nAvg. Grade: %.2f\nCourse: %s\n",A2->number,A2->year,A2->grade,A2->course);
return 1;
}

You might find some bad tagged functions in the code as I've just translated it to english, and some I might have passed throught without changing. I believe that's not the problem ofc. Sorry for my bad english, as you can see it's not my main language... Thanks in advance!

--EDIT-- As asked in the comments, here is the Alumn.h file code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
    int num;
    int year;
    char course[30];
    float grade;
}ALUMN;

ALUMN *copyAlumn(ALUMN *A){
    ALUMN *B;

    B = (ALUMN *)malloc(sizeof(ALUMN));

    if(B==NULL)
        return NULL;

    //*B=*A;
    (*B).num=(*A).num;
    (*B).year = (*A).year;
    (*B).grade = (*A).grade;
    strcpy((*B).course,(*A).course);

    return B;
}

Solution

  • In

    scanf("%f",&A1.grade);
    printf("Insert its course: ");
    gets(A1.course);
    

    when you enter the value for grade you finished it by a newline (e.g. 12.23<enter>) the part 12.23 is used by scanf and the newline still present in the input is get by gets who returns an empty string

    is a bad idea to mix scanf and gets


    you can replace

    printf("Alumn A1:\nInsert the alumn number: ");
    scanf("%d",&A1.num);
    printf("Insert the year of entrance: ");
    scanf("%d",&A1.year);
    printf("Insert its avg. grade: ");
    scanf("%f",&A1.grade);
    printf("Insert its course: ");
    gets(A1.course);
    

    by

    char s[16];
    
    printf("Alumn A1:\nInsert the alumn number: ");
    if ((fgets(s, sizeof(s), stdin) == NULL) ||
        (sscanf(s, "%d",&A1.num) != 1)) {
      /* indicate error */
      return -1;
    }
    
    printf("Insert the year of entrance: ");
    if ((fgets(s, sizeof(s), stdin) == NULL) ||
        (sscanf(s, "%d",&A1.year) != 1)) {
      /* indicate error */
      return -1;
    }
    
    printf("Insert its avg. grade: ");
    if ((fgets(s, sizeof(s), stdin) == NULL) ||
        (sscanf("%f",&A1.grade) != 1)) {
      /* indicate error */
      return -1;
    }
    
    printf("Insert its course: ");
    if (fgets(A1.course, sizeof(A1.course), stdin) == NULL) {
      /* indicate error */
      return -1;
    }
    

    as you see I use fgets rather than gets to be sure to not go out of the string with an undefined behavior