Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过输入空行退出循环_C++ - Fatal编程技术网

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 );
}