Dev-C++ Input skipped

前端 未结 3 759
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<stdio.h>
    
      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();
        }
    
    0 讨论(0)
  • 2021-01-21 11:39

    With some little research, I guess that the problem comes with scanf(). scanf() reads a line without the end of line character '\n' which seems to stay in the buffer and actually red by the next statement.

    alternatively you can use fgets() and sscanf() as follows:

    To read a character I used:

    fgets(text,sizeof(text),stdin);
    sscanf(text,"%c",&c); /* or: c = text[0]; */
    

    to read an integer I have used

    fgets(text,sizeof(text),stdin);
    sscanf(text,"%d",&i);
    

    I had a major problem with gets() in a C course I had (to which DevC++) was advised as a compiler. However, I totally recall I didn't follow the advice and it turned out that the behavior of fgets() is also compiler dependent.

    The man page for gets() has this:

    BUGS

    Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

    0 讨论(0)
  • 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')
        ;
    
    0 讨论(0)
提交回复
热议问题