Ad

Is There Any Way To Stop The Previous Working Inputs During Using A Loop Or In Any Other Case?

- 1 answer

When I use fgets within a loop then it takes the previous working enter command as input and do the processing for it and this happens with me not with only fgets but also in many other situations so my question is....

1.Is there a way to stop the previous working commands while starting any loop?

2.if not then please tell how can I resolve this issue?

PROGRAM

#include <stdio.h>
#include <strings.h>

struct student
{
    char name[10];
}st[10];


int main()
{
    int i,j;
    printf("enter the number of students=");
    scanf("%d",&i);
    j=i;

    for (i=1;i<=j;i++)
    {
        printf("enter the student's name= ");
        fgets(st[i].name,sizeof(st[i]),stdin);
        printf("%s",st[i].name);
    }

}

OUTPUT

enter the number of students=2                                                                     
enter the student's name=                                                                          
enter the student's name= hello                                                                    
hello    

see here it takes input from 2nd line i.e-it takes the first input as a enter key and shift to 2nd input

Ad

Answer

You can do it by using fflush to flush stdin buffer...see the code below:

#include <stdio.h>
#include <strings.h>

struct student
{
    char name[10];
}st[10];


int main()
{
    int i,j;
    printf("enter the number of students=");
    scanf("%d",&i);
    j=i;

    for (i=1;i<=j;i++)
    {
        
        fflush(stdin); // flush the stdin buffer

        printf("enter the student's name= ");
        fgets(st[i].name,sizeof(st[i]),stdin);
        printf("%s",st[i].name);
    }
    return 0;
}

output is:

enter the number of students=2
enter the student's name= hello
hello
enter the student's name= world
world

The problem of using fflush(stdin):

Though fflush(stdin) works, its actually not the perfect solution. There are some potential problem with fflush(stdin). Please take a look here. And in case of online gdb, fflush(stdin) not working at all.

So, I'm proposing one alternative solution. It'll work perfectly in both online gdb and also in your local pc:

The problem is the uneaten '\n' at the end entering number. This is kept in the buffer of stdin. So, we just have to eat it. So, we'll just write a scanf("%d%c", &i, &uneaten_newline_char). The full code:

#include <stdio.h>
#include <strings.h>

struct student
{
    char name[10];
} st[10];


int main()
{
    char uneaten_newline_char;
    int i,j;
    printf("enter the number of students=");
    scanf("%d%c", &i, &uneaten_newline_char); // notice, there's no space between '%d%c', they must be written without any space between them
    j=i;

    for (i=1;i<=j;i++)
    {
        
        // fflush(stdin); // flush the stdin buffer

        printf("enter the student's name= ");
        fgets(st[i].name, sizeof(st[i]), stdin);
        
        printf("%s",st[i].name);
    }
    return 0;
}

This solution may look a bit ugly...but this is better than using fflush(stdin). But the most beautiful and perfect solution for me would be using your own reader function. Create a readline function of your own. And handle spaces and newlines at the end as you need. c's builtin functions definitely is not great in this case.

Ad
source: stackoverflow.com
Ad