Dev-C++ Input skipped

前端 未结 3 761
Happy的楠姐
Happy的楠姐 2021-01-21 11:09
#include
#include
main()
{
      int i;
      char c, text[30];
      float f;
      printf(\"\\nEnter Integer : \");
      scanf(\"%d\",&a         


        
3条回答
  •  囚心锁ツ
    2021-01-21 11:46

    When you type 42 (or whatever) as the first integer, you actually type three characters: 4, 2 and then the newline character that comes from pressing ENTER. Your first scanf reads an integer, which means that it only reads the 4 and the 2, leaving the newline character in the input buffer.

    When your program gets to gets, it reads a the very short line that consists just of that newline character.

    You can fix it by reading and throwing away the newline character just after scanf, something like this:

    printf("\nEnter Integer : ");
    scanf("%d",&i);
    while (getchar() != '\n')
        ;
    

提交回复
热议问题