0

The code snippet is

#include <stdio.h>

int main() {
  char another = 'y';
  if (another == 'y') {
    printf("Add Another? (y/n)\n");
    scanf("%c", &another);
    main();
  } else if (another == 'n') {
    return 1;
  } else {
    main();
  }
}

This is a part of somewhat larger program, In that also it is prompted twice for each iteration. Please tell me where the problem is? Thanks in advance.

Eraklon
  • 4,206
  • 2
  • 13
  • 29

2 Answers2

0

Because when you input a character then hit an enter the new line character from hitting the enter is also will be in the input buffer. You read out the character, so the new line remains there. Therefore in the next iteration scanf will read this new line character. Put a space like this " %c" to avoid this. This way leading whitespace characters will be ignored.

Eraklon
  • 4,206
  • 2
  • 13
  • 29
0

1, As @Eraklon explains.

2, At the begin of main function, you assign:

char another = 'y';

So, When you call main() again, anotheris always equal to 'y'. So, your program will never stop.

Hitokiri
  • 3,607
  • 1
  • 9
  • 29