在询问用户输入时,C程序跳过一行

时间:2015-05-11 10:53:43

标签: c char user-input scanf

CakePHP is NOT able to connect to the database.

Connection to database could not be established: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)

当我为曲目数量输入5时,程序会显示这是一张专辑还是单曲并在那里结束程序而不让我输入专辑类型?

1 个答案:

答案 0 :(得分:2)

第1点

请勿使用fflush( stdin );,它是undefined behaviour

相关:来自C11标准docmunet,第7.21.5.2章,(强调我的)

  

int fflush(FILE *stream);

     

如果stream指向输入流或未输入最新操作的更新流,则fflush函数会将该流的任何未写入数据传递到主机环境以写入文件; 否则,行为未定义。

第2点 (通过fflush(stdin)完成禁止完成的工作

更改

scanf("%c", &type);

scanf(" %c", &type);
      ^
      |

前导空格忽略缓冲区中类似空格的字符,并从stdin读取第一个非空格字符。

相关问题