作者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://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1543313291.A.F1F.html
1F:→ djshen: 你确定没按enter前真的有break? 11/27 18:17
我懂了 按下Cmd+V是跑出答案
再按Enter才是break
※ 编辑: nthulibrary (140.114.123.135), 11/27/2018 18:24:20
2F:推 b0920075: c++字串可以这样比? 11/27 23:52
3F:推 b0920075: 原来可以,我一直以为要string.compare() 11/27 23:58
4F:→ tomsawyer: =='E'(?) 11/28 19:49
5F:推 Paravion: string有overload operators 可以直接比 11/28 22:14
6F:→ Paravion: 'E'会返回int “E”才能比 11/28 22:15
7F:→ eye5002003: 看了一下cplusplus.com还真的只提到compare而已 11/29 08:38
8F:→ eye5002003: cppreference.com就有写 11/29 08:39
9F:推 longlongint: 你确定互动中没按enter 可以离开cin那行吗 12/06 11:02