看板 C_and_CPP 關於我們 聯絡資訊
大家好,小弟最近看網路上的C++ project tic tac toe的遊戲程式,網址如下 http://www.cppforschool.com/project/tic-tac-toe-project.html 其中 else { cout<<"Invalid move "; player--; cin.ignore(); cin.get(); } 跟 if(i==1) cout<<"==>\aPlayer "<<--player<<" win "; else cout<<"==>\aGame draw"; cin.ignore(); cin.get(); return 0; 都用了 cin.ignore(); cin.get(); 的用法 請問一下這邊ignore是要丟掉什麼東西? cin.get()是接收enter鍵吧? 我試著把cin.ignore()這行刪去,程式的提示訊息就不會定格了 但我還是不知道這行處理的什麼事 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.239.50.45 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1461033670.A.49F.html
longlongint: 收到enter就不會定格了(? 04/19 11:02
tuyutd0505: ignore是忽略enter. get是為了製造system("pause")的 04/19 11:59
tuyutd0505: 效果 04/19 11:59
sinlin: 那請問這個ignore為什麼只加在else裡面 04/19 15:11
sinlin: 而不是cin.get<<choice後面呢? 04/19 15:12
sinlin: 上面打錯,我是指cin>>choice後面 04/19 15:21
tuyutd0505: 把ignore都砍掉 只移到cin後面也可以達到一樣效果 04/19 17:49
tuyutd0505: 就純粹寫法喜好而已 04/19 17:50
sinlin: 試了一下果然如此,謝謝 04/20 18:36