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

作者: nthulibrary (Entropy)   2018-11-27 18:08:09
開發平台(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"
要怎麼確實的跳出迴圈然後結束程式?
作者: djshen (djshen)   2018-11-27 18:17:00
你確定沒按enter前真的有break?
作者: b0920075 (Void)   2018-11-27 23:52:00
c++字串可以這樣比?原來可以,我一直以為要string.compare()
作者: tomsawyer (安安)   2018-11-28 19:49:00
=='E'(?)
作者: Paravion (ElonMusk)   2018-11-28 22:14:00
string有overload operators 可以直接比'E'會返回int “E”才能比
作者: eye5002003 (下一夜)   2018-11-29 08:38:00
看了一下cplusplus.com還真的只提到compare而已cppreference.com就有寫
作者: longlongint (華哥爾)   2018-12-06 11:02:00
你確定互動中沒按enter 可以離開cin那行嗎

Links booklink

Contact Us: admin [ a t ] ucptt.com