[問題] break while後 while仍持續進行條件判斷

看板C_and_CPP作者 (過氣wifi)時間6年前發表 (2018/11/27 10:08), 6年前編輯推噓4(405)
留言9則, 6人參與, 6年前最新討論串1/1
開發平台(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

11/27 18:17, 6年前 , 1F
你確定沒按enter前真的有break?
11/27 18:17, 1F
我懂了 按下Cmd+V是跑出答案 再按Enter才是break ※ 編輯: nthulibrary (140.114.123.135), 11/27/2018 18:24:20

11/27 23:52, 6年前 , 2F
c++字串可以這樣比?
11/27 23:52, 2F

11/27 23:58, 6年前 , 3F
原來可以,我一直以為要string.compare()
11/27 23:58, 3F

11/28 19:49, 6年前 , 4F
=='E'(?)
11/28 19:49, 4F

11/28 22:14, 6年前 , 5F
string有overload operators 可以直接比
11/28 22:14, 5F

11/28 22:15, 6年前 , 6F
'E'會返回int “E”才能比
11/28 22:15, 6F

11/29 08:38, 6年前 , 7F
看了一下cplusplus.com還真的只提到compare而已
11/29 08:38, 7F

11/29 08:39, 6年前 , 8F
cppreference.com就有寫
11/29 08:39, 8F

12/06 11:02, 6年前 , 9F
你確定互動中沒按enter 可以離開cin那行嗎
12/06 11:02, 9F
文章代碼(AID): #1R_HUByV (C_and_CPP)