通过输入空行退出循环
Exit loop by entering blank line
即使输入空行,循环也不中断。
string temp;
cin >> temp;
while (!temp.empty()) {
cout<<"Hello"<<endl;
cin>>temp;
}
当我没有输入而只按enter时,它应该退出循环。
尝试break
命令continue
将跳过循环的当前迭代
当没有字符可以从input
stream
中读取时,cin
抛出flag
failbit
,并且永远不会继续尝试从input
stream
中读取extract
字符。你可以通过getline( cin, temp )
实现你的目标。getline()
读取到stream
中的deliminating
character
,然后丢弃它,即使stream
中没有其他字符,留下temp
为空,并从temp.empty()
返回0
。
string temp;
getline( cin, temp );
while (!temp.empty()) {
cout<<"Hello"<<endl;
getline( cin, temp );
}
http://www.cplusplus.com/reference/istream/istream/operator-free/
相关文章:
- C++ 自动点击器未退出循环
- 使用 while 或 do while 退出循环
- 退出循环,除非未给出输入
- C++线程退出循环条件
- 在C++测试家庭作业问题中的字符串输入时无法退出循环(作为数据验证)
- 虽然具有多个 OR 条件的循环不退出循环
- 如果程序退出循环是goto已删除的本地变量
- 中断语句未退出循环
- 计算平均值,而不向退出循环添加负等级
- 使用键盘挂钩时无法动态退出循环
- 定义时间后退出C 循环
- 使用goto来干净地退出循环
- getchar 有什么问题(因为程序在没有检查的情况下退出循环)
- 尝试退出循环时,Ctrl+Z 不起作用
- 如何获取用户输入以退出循环
- 正在销毁线程并退出循环
- 中断语句不会退出循环
- 通过输入一个特定的字母c++来退出循环
- 在c++中,当输入不是unsigned时立即退出循环
- 编程原理和实践使用C++while-loop演练。找不到退出循环的方法