Dev-C++ Input skipped

前端 未结 3 758
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条回答
  •  -上瘾入骨i
    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.

提交回复
热议问题