C++ 如何检查用户何时按enter键而不提供C+中的值+;

C++ 如何检查用户何时按enter键而不提供C+中的值+;,c++,input,key,C++,Input,Key,在我的代码中,我想检查在未输入输入值时是否按下了enter键。因为,通常按回车键只会生成新行,除非输入未被输入,否则;相反,在这种情况下,我希望它通过检测按下enter键来发出命令 #include <iostream> #include <string> using namespace std; int main(){ String str=""; while(str!="exit"){

在我的代码中,我想检查在未输入输入值时是否按下了enter键。因为,通常按回车键只会生成新行,除非输入未被输入,否则;相反,在这种情况下,我希望它通过检测按下enter键来发出命令

   #include <iostream>
   #include <string>
   using namespace std;

   int main(){    
        String str="";
        while(str!="exit"){
          cin>>str;
          if(input is not entered and enter key is pressed)
             continue;
          else
             break;
        }
        return 0;
    }
#包括
#包括
使用名称空间std;
int main(){
字符串str=“”;
while(str!=“退出”){
cin>>str;
如果(未输入,按enter键)
继续;
其他的
打破
}
返回0;
}
我愿意接受任何建议

#包括
#include<iostream>
#include<conio.h>
#include <string>
using namespace std;
int main()
{    
 char str[10];
 char a;
 int i=0;
 cout<<"\nEnter the input :-";
 do{
      a=getche();
      if(a!=13)
        {
         str[i]=a;
         i++;
        }
        else
          break;
   }
   while(a!=13);
   return 0;
}   
#包括 #包括 使用名称空间std; int main() { char-str[10]; 字符a; int i=0;
cout这里有一个完整的示例。简而言之,您可以使用
中的
getline
,当您点击
Enter
键时,它也会为您提供空输入

#include <iostream>
#include <string>

int main()
{
    while(true)
    {
        std::string in;
        getline(std::cin, in);

        if (in.empty())
        {
            std::cout << "Enter key was pressed with no message" << std::endl;
        }
        else
        {
            std::cout << "Enter key was pressed with message" << in << std::endl;
        }
    }
    return 0;
}
#包括
#包括
int main()
{
while(true)
{
std::字符串输入;
getline(标准::cin,in);
if(in.empty())
{

std::cout您可以使用getch()获取字符或任何其他亲属,然后使用ANSCI代码“\x0D”比较enter键。

Hi Encoder Senjin。我最近遇到了这个问题,Darien Pardinas的答案对我有效。如果它对你有效,你应该正式接受它,以便其他人可以看到。如果它不起作用,具体会发生什么?你是如何修复的?谢谢。