Question: I have the following piece of code where **check** is for char type

```
do{
      printf("Do you want to add an edge(n to stop adding)\n");
      check = getchar();
}while(check!='n');

```

and

```
do{
      printf("Do you want to add an edge(n to stop adding)\n");
      scanf("%c",&check);

}while(check!='n');

```

Both of them produce the same output

For example for the **r\n** I get **Do you want to add an edge** twice and for say **tt\n** I get the same question thrice followed by asking the input. 

My question is how to avoid it and why is it happening?

Answer: 
To solve your issue, try this:

```
void main()
{
    char check;
    char c;
    do
    {
        printf("Do you want to add an edge(n to stop adding)\n");
        check = getchar();
        do { c = getchar(); } while (c != '\n' && c != EOF);  // This gobbles the rest of the chars on stdin
    } while(check != 'n' && check != EOF);
}

```
You have to make sure that `check` is alphanumeric, as `\n` is not ignored by `scanf` or `getchar()`.

```
do {
    printf("Do you want to add an edge(n to stop adding)\n");
    do
    {
        check = getchar();
    } while (!isalnum(check) && check != EOF);
} while(check!='n');

```

EDIT: Because you wanted only the first character in a line:

```
char line[64];
fgets(line, 64, stdin);
check = line[0]; 

```

You still have to check to make sure that `check` is within the bounds of your input.