[問題] cin.ignore()和cin.get()

看板C_and_CPP (C/C++)作者 (新生活!~)時間10年前 (2016/04/19 10:41), 編輯推噓3(306)
留言9則, 3人參與, 最新討論串1/1
大家好,小弟最近看網路上的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

04/19 11:02, , 1F
收到enter就不會定格了(?
04/19 11:02, 1F

04/19 11:59, , 2F
ignore是忽略enter. get是為了製造system("pause")的
04/19 11:59, 2F

04/19 11:59, , 3F
效果
04/19 11:59, 3F

04/19 15:11, , 4F
那請問這個ignore為什麼只加在else裡面
04/19 15:11, 4F

04/19 15:12, , 5F
而不是cin.get<<choice後面呢?
04/19 15:12, 5F

04/19 15:21, , 6F
上面打錯,我是指cin>>choice後面
04/19 15:21, 6F

04/19 17:49, , 7F
把ignore都砍掉 只移到cin後面也可以達到一樣效果
04/19 17:49, 7F

04/19 17:50, , 8F
就純粹寫法喜好而已
04/19 17:50, 8F

04/20 18:36, , 9F
試了一下果然如此,謝謝
04/20 18:36, 9F
文章代碼(AID): #1N5Ph6IV (C_and_CPP)
文章代碼(AID): #1N5Ph6IV (C_and_CPP)