-1

It is supposed to get 10 names and for each name there should be an input of 4 scores but it only works on the first loop and the function "gets" does not work on the next following loops.

#include <conio.h>
int main(){
    
    int name_ctr,score_ctr,score;
    char name[50];
    
    for(name_ctr=1; name_ctr<=10; name_ctr++){
        printf("Enter the student's name: ");
        gets(name);
        for(score_ctr=1;score_ctr<=4;score_ctr++){
            printf("Enter the studet's scores: ");
            scanf("%d",&score);
        }
    }
    
    return 0;
}
Minju
  • 1
  • 2
  • 2
    Not what you are actually asking about but never use `gets`. Use `fgets` instead. [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). – kaylum Feb 28 '22 at 02:51
  • 1
    As for the actual problem, mixing `fgets` and `scanf` is not a good idea. `scanf` leaves the trailing newline character which will be interpreted as a blank line by the next `fgets` call. Suggest replacing `scanf` with `fgets` followed by `sscanf`. – kaylum Feb 28 '22 at 02:53
  • My class did not talked about those functions yet and I'm still kind of confused about the fgets syntax but I kind get your point and Ill try to learn and see. – Minju Feb 28 '22 at 02:58
  • Minju, `scanf("%d",&score);` does **not** read a _line_ of user input. `gets()` does read a _line_ and so gets the portion of the line left by `scanf()`. Don't use `gets()`. Don't use `scanf()` until you know why it is bad. Use `fgets()` and then parse that input. Sorry that your class instruction is poor. – chux - Reinstate Monica Feb 28 '22 at 03:18

1 Answers1

-2

add int c ; while((c=getchar())!='\n'){} after second inner loop the program will work fine. gets() read string until it encounter a newline so at the second iteration it were reading '\n' left in input buffer due to scanf. There is no buffer overflow

  • will using fflush() inside the loop or somewhere fix it? or is it unrelated to the problem? – Minju Feb 28 '22 at 03:00
  • What evidence do you have of that? Seems more like a speculative comment rather than a definete answer. – kaylum Feb 28 '22 at 03:16
  • add int c ; while((c=getchar())!='\n'){} after second inner loop the program will work fine. gets() read string until it encounter a newline so at the second iteration it were reading '\n' left in input buffer due to scanf. There is no buffer overflow. – rishabh pandey Feb 28 '22 at 03:19
  • @rishabhpandey [`while((c=getchar())!='\n'){}`](https://stackoverflow.com/questions/71290154/function-gets-not-working-on-the-following-loops#comment126013303_71290196) is an infinite loop on end-of-file, thus not a good solution. – chux - Reinstate Monica Feb 28 '22 at 03:24
  • @chux-ReinstateMonica what's the problem if the code is working fine. ? – rishabh pandey Feb 28 '22 at 03:32
  • @rishabhpandey What value do expect `c` to be when end-of-file occurs? – chux - Reinstate Monica Feb 28 '22 at 04:44