作者nthulibrary (過氣wifi)
看板C_and_CPP
標題[問題] break while後 while仍持續進行條件判斷
時間Tue Nov 27 18:08:09 2018
開發平台(Platform): (Ex: Win10, Linux, ...)
macOS
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
G++ 8
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
none
問題(Question):
int main在return 0後,terminal不會關掉程式
餵入的資料(Input):
n/a
預期的正確結果(Expected Output):
跳出while 結束程式
錯誤結果(Wrong Output):
要手動按enter才會跳
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
int main(){
string cmd;
while(cin >> cmd){
if(cmd == "E") break;
else //do something;
}
return 0;
}
補充說明(Supplement):
設計是讓程式讀到"E"後就跳出while,然後直接return 0;
可是看來break之後它還在"cin >> cmd"
要怎麼確實的跳出迴圈然後結束程式?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.114.123.135
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1543313291.A.F1F.html
→ djshen: 你確定沒按enter前真的有break? 11/27 18:17
我懂了 按下Cmd+V是跑出答案
再按Enter才是break
※ 編輯: nthulibrary (140.114.123.135), 11/27/2018 18:24:20
推 b0920075: c++字串可以這樣比? 11/27 23:52
推 b0920075: 原來可以,我一直以為要string.compare() 11/27 23:58
→ tomsawyer: =='E'(?) 11/28 19:49
推 Paravion: string有overload operators 可以直接比 11/28 22:14
→ Paravion: 'E'會返回int “E”才能比 11/28 22:15
→ eye5002003: 看了一下cplusplus.com還真的只提到compare而已 11/29 08:38
→ eye5002003: cppreference.com就有寫 11/29 08:39
推 longlongint: 你確定互動中沒按enter 可以離開cin那行嗎 12/06 11:02