为何这个break没有终止while
为什么这个break没有终止while?
我输入一个空格,循环没有结束,为什么?
------解决方案--------------------
cin会自动跳过空格,你编程测试看看。
------解决方案--------------------
使用cin.get(ch)
------解决方案--------------------
cin >> ch,在默认情况下会跳过刚开始的空格(如果有的话),这项功能可以关闭。
------解决方案--------------------
cin默认会跳过空格,所以程序还在等你的输入,当然跳不出喽。貌似5楼给出了解决方案,不过我没有验证。
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
while(cin>>ch)
{
if(isspace(ch))
{
break;
}
}
return 0;
}
我输入一个空格,循环没有结束,为什么?
------解决方案--------------------
cin会自动跳过空格,你编程测试看看。
------解决方案--------------------
使用cin.get(ch)
------解决方案--------------------
cin >> ch,在默认情况下会跳过刚开始的空格(如果有的话),这项功能可以关闭。
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char ch;
while (cin >> std::noskipws >> ch)
{
if (isspace(ch))
{
break;
}
}
return 0;
}
------解决方案--------------------
cin默认会跳过空格,所以程序还在等你的输入,当然跳不出喽。貌似5楼给出了解决方案,不过我没有验证。