看板 C_and_CPP 關於我們 聯絡資訊
這原本是我程式的一部分 經過我找尋之後的結果 發現以下的問題 #include <stdio.h> #include <stdlib.h> int main() { char words[100]; int row; scanf("%d",&row); // 這行好像會吃掉下面的一個gets() printf("你要編輯第%d行:\n",row); // 單純輸出而已 gets(words); printf("%s\n",words); system("pause"); return 0; } 照理講這樣執行的結果應該是 ----------------------------- 8 你要編輯第8行 This is a book. This is a book. ----------------------------- 但是經過我多次執行後發現 這樣寫程式不會執行 get(words); 這行指令 要嘛就把前面的 scanf("%d",&row); 刪掉 要嘛就在下面多加一行 gets(words); 就是變成 scanf("%d",&row); printf("你要編輯第%d行:\n",row); gets(words); gets(words); printf("%s\n",words); 這樣執行就會照我所想的 但是這樣很奇怪... 為什麼 scanf() 會吃掉一行 gets() ? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.171.74.223
LPH66:因為 scanf 的 %d 不會把你的 Enter 吃掉 04/20 08:15
可是我是按完ENTE後 程式才執行到 gets(words); 這行 這樣會有關系嗎? ※ 編輯: sarsenwen 來自: 118.171.74.223 (04/20 12:12)
MOONRAKER:當然有關係,因為這樣enter就被gets()吃進去了。 04/20 12:38