Dev-C++ Input skipped

前端 未结 3 763
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:33

    Scanf or other input parsing functions take only required quantity of characters as specified in the call from stdin and reject others.As a result these rejected values,during next read of stdin enter into the variables along with the newline characters and thus skipping inputs for a few calls.So its better to call a clear routine that cleans stdin and stops garbage entering into other variables.

    Although your code is quite vulnerable still it has solution:-

    #include
    
      int clear()
      {
        while ((getchar())^'\n');    
      }
      int  main()
        {
              int i;
              char c, text[30]={0};
              float f;
              printf("\nEnter Integer : ");
              scanf(" %d",&i);
              printf("\nEnter Character : ");     
              scanf(" %c",&c);    
              printf("\nEnter String:");
          clear();
              gets(text);
              printf("\nEnter Float:");   
              scanf(" %f",&f);    
              printf("\nInteger : %d",i);
              printf("\nCharacter : %c",c);
              printf("\nString : %s",text);
              printf("\nFloat : %f",f);
              getchar();
        }
    

提交回复
热议问题