0

this is a follow up question of another one that I asked yesterday.

The goal of the code is to ask the user for the name of one student, 4 of their subjects and their respective grade of each subject.

The thing is: The program succesfully ask the user for the name, the first subject and the first respective grade. But when the program goes to ask for the second subject name, it shows the message asking the user for it, then ignores the 'gets' and immediatly shows the error message that no valid name was input, then shows the message asking the user again for the name, and this time let the user type in.


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

main(){
    printf("Este programa captura el nombre de un alumno \n");
    printf  ("y cuatro de sus materias y sus respectivas notas\n");
    printf      ("Nota: El programa solo toma en cuenta los dos primeros\n");
    printf          ("decimales de la notas expresada.\n\n");
    
    char alumno[40] = {'\0'};
    char mat[4][20] = {'\0', '\0', '\0', '\0'};
    float calif[4] = {-1.0, -1.0, -1.0, -1.0};
    int i;
    
    while (alumno[0] == '\0'){
        printf("Ingresa el nombre del alumno: ");
        gets(alumno);
        if(alumno[0] == '\0'){
            printf("\nError. El alumno debe de llevar un nombre.");
            printf  ("\nTrata nuevamente\n\n");
        };
    };
    for(i = 0; i < 4; ++i){
        while (mat[i][0] == '\0'){
            printf("Ingresa el nombre de la materia %d: ", i+1);
            gets(mat[i]);
            if(mat[i][0] == '\0'){
                printf("\nError. Las materias deben ser declaradas.");
                printf  ("\nTrata nuevamente.\n\n");
            };
        };
        while (calif[i]<0 || calif[i]>10){
            printf("Ingrese la nota correspondiente a esta materia (0-10): ");
            scanf("%f", &calif[i]);
            if(calif[i]<0 || calif[i]>10){
                printf("\nError. Debe ingresar una nota entre 0 y 10.");
                printf  ("\nTrata nuevamente.\n\n");
            };
        };
    };
    
    return 0;
};

What's wrong with the code?

  • This seems to be a problem with mixing the input methods. Please see [fgets() doesn't work after scanf](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf). I see nobody mentioned in the previous question that `gets` is no longer part of the standard C library. Please read [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) But anyway, the same applies with mixing input methods. – Weather Vane Aug 07 '20 at 17:26
  • Aside: `main()` should be `int main(void)` – Weather Vane Aug 07 '20 at 17:31
  • After the last while loop, you need to process the 'return' or 'enter' key input. When you press Enter after entering the last input it reads the value into the variable but there is one more value left on the stack to process which is the 'return' key. So when the next 'gets' is called it takes that left over 'return' key and moves ahead and hence you don't have a valid input which then results in error. Using "getchar()" after the last while loop should work. – golimoli Aug 07 '20 at 17:37
  • @AbhishekChoubey fudging it with a `getchar()` kludge isn't the way to go: better to be consistent with the input methods. The generally recommended way is to take each line input with `fgets()` and process it with `sscanf()`. – Weather Vane Aug 07 '20 at 17:46
  • 1
    The program should ignore *all* instances of `gets`! – William Pursell Aug 07 '20 at 17:49
  • @WeatherVane : Agreed. I thought its easier to explain what's wrong here with a getchar. Other than that, using the recommended options is the way to go. – golimoli Aug 07 '20 at 17:54

0 Answers0