C++ 通过输入空行退出循环
输入空行后,循环不会中断C++ 通过输入空行退出循环,c++,C++,输入空行后,循环不会中断 string temp; cin >> temp; while (!temp.empty()) { cout<<"Hello"<<endl; cin>>temp; } 字符串温度; cin>>温度; 而(!temp.empty()){ cout尝试break命令 continue跳过循环的当前迭代cin在输入流中没有可读取的字符时抛出标志故障位,并且永远不要继续尝试从输入流中提取字符。您可以用getlin
string temp;
cin >> temp;
while (!temp.empty()) {
cout<<"Hello"<<endl;
cin>>temp;
}
字符串温度;
cin>>温度;
而(!temp.empty()){
cout尝试break
命令
continue
跳过循环的当前迭代cin
在输入
流
中没有可读取的字符时抛出标志
故障位
,并且永远不要继续尝试从输入
流
中提取字符。您可以用getline(cin,temp)
getline()
读取流
中的字符
,然后丢弃它,即使流
中没有其他字符,保留temp
为空,并从temp.empty()返回0
)
字符串温度;
getline(cin,temp);
而(!temp.empty()){
coutif(input==“”){/*do things*/}适用于meq的工具可能未初始化。无论如何,解决此问题的正确工具是调试程序是否尝试检查“temp”按下“仅输入”后的值?我怀疑在cin>>temp之前初始化为空字符串应该会有所帮助。请为您的答案提供更多详细信息。我希望用户输入输入直到他输入一个空行。请提供详细信息。
string temp;
getline( cin, temp );
while (!temp.empty()) {
cout<<"Hello"<<endl;
getline( cin, temp );
}