在使用scanf()之後,我浪費了太多時間來搜索爲什麼我的程序沒有執行gets(),我找到了一個解決方案在scanf()之後使用fflush(stdin)來啓用gets()來獲取字符串。程序在scanf()之後不執行gets(),甚至使用fflush(stdin)
問題是,fflush(stdin)沒有做到它所期望的:程序繼續跳過gets(),我無法在控制檯中寫入任何短語來讀取。
我的代碼是下一個:
#include <string.h>
#include <stdio.h>
int main(){
char nombre[10];
char mensaje[80];
printf("Type your name:\n");
scanf("%s", nombre);
fflush(stdin);
printf("Now, type a message:\n");
gets(mensaje);
printf("3/%s:%s",nombre,mensaje);
return 0;
}
請閱讀:http://stackoverflow.com/questions/2979209/using-fflushstdin(儘管與您的問題無關)。 – Mat
獲取之前可能在[Scanf中的輸入重複。問題](http://stackoverflow.com/questions/2366509/input-in-c-scanf-before-gets-problem) – Mat
將上一個'printf'改爲'printf(「3 /%s:%s \ n 「,nombre,mensaje);' –