C++检测用户按下 ENTER 键

发布于 2024-08-31 19:14:54 字数 512 浏览 10 评论 0原文

我有一个循环,要求用户输入名称。当用户按下 ENTER 键或输入 20 个名称时,我需要停止。但是,当用户按 ENTER 键时,我的方法不会停止

//loop until ENTER key is entered or 20 elements have been added
bool stop = false;
int ind = 0;
while( !stop || ind >= 20 ){

    cout << "Enter name #" << (ind+1) << ":";
    string temp;
    getline(cin, temp);
    int enterKey = atoi(temp.c_str());        

    if(enterKey == '\n'){
        stop = true;            
    }
    else{
        names[ind] = temp;
    }

    ind++;


}

I have a loop where I ask the user to enter a name. I need to stop when the user presses the ENTER key..... or when 20 names have been entered. However my method doesn't stop when the user presses the ENTER key

//loop until ENTER key is entered or 20 elements have been added
bool stop = false;
int ind = 0;
while( !stop || ind >= 20 ){

    cout << "Enter name #" << (ind+1) << ":";
    string temp;
    getline(cin, temp);
    int enterKey = atoi(temp.c_str());        

    if(enterKey == '\n'){
        stop = true;            
    }
    else{
        names[ind] = temp;
    }

    ind++;


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回梦 2024-09-07 19:14:54

您可以使用 atoi 将读取的字符串转换为整数:

int enterKey = atoi(temp.c_str());        

如果 temp 是类似 "1234" 的字符串,则会将 enterKey 设置为 1234。然后将 enterKey\n 的 ASCII 值进行比较。这很可能没有做任何有用的事情。

另外,std::getline 仅读取字符,但不包括下一个'\n'。如果用户只按 Enter 键而不输入任何其他字符,std::getline 将返回空字符串。可以使用其 empty() 方法轻松测试字符串是否为空:

getline(cin, temp);
if (temp.empty()) {
  stop = true;
}

You convert the read string to an integer with atoi:

int enterKey = atoi(temp.c_str());        

If temp is a string like "1234" this will set enterKey to 1234. Then you compare enterKey to the ASCII value of \n. This is most probably not doing anything useful.

Also std::getline just read the characters up to, but not including, the next '\n'. If a user just presses enter without typing any other characters, std::getline will return an empty string. If a string is empty can be easily tested with its empty() method:

getline(cin, temp);
if (temp.empty()) {
  stop = true;
}
慢慢从新开始 2024-09-07 19:14:54

getline 会吃掉您的分隔符,即“\n”,因此您可能需要检查空字符串。在调用 atoi 之前执行此操作。

getline will eat your delimiter, which will be '\n', so you probably want to be checking for an empty string. Do it before the call to atoi.

多情癖 2024-09-07 19:14:54

尝试:

while( !stop && ind < 20 )

或:

using namespace std;
vector <string> names; // edited.
for (int ind = 0; ind < 20; ++ind)
{
    cout << "Enter name #" << (ind+1) << ":"; 
    string temp;
    getline(cin, temp); 
    if (temp.empty())
        break;
    names.push_back(temp);
}

try:

while( !stop && ind < 20 )

or:

using namespace std;
vector <string> names; // edited.
for (int ind = 0; ind < 20; ++ind)
{
    cout << "Enter name #" << (ind+1) << ":"; 
    string temp;
    getline(cin, temp); 
    if (temp.empty())
        break;
    names.push_back(temp);
}
我ぃ本無心為│何有愛 2024-09-07 19:14:54

尝试使用 stop = temp.empty() 来代替。 getline 不应包含任何换行符。空行应该产生空字符串。

另外,查尔斯是正确的,您的 while 条件不正确,请使用 while(!stop && ind <20)。按照您编写的方式,用户需要输入 20 个值和一个空行。查尔斯的更改表示,当满足其中一个条件(而不是同时满足两个条件)时就中断。

为了完整起见,这里是建议的新代码:

bool stop = false;
int ind = 0;
while( !stop && ind < 20 ){

    cout << "Enter name #" << (ind+1) << ":";
    string temp;
    getline(cin, temp);
    if(temp.empty()) {
        stop = true;
    } else {
        names[ind] = temp;
    }

    ind++;    
}

就我个人而言,我会编写如下代码:

vector<string> names;
for(int ind = 0; ind < 20; ind++) {
  cout << "Enter name #" << (ind + 1) << " (blank to stop): ";
  string name;
  getline(cin, name);
  if(name.empty() || cin.eof()) {
     break;
  }
  names.push_back(name);
}

cout << "Read " << names.length() << " names before empty line detected." << endl;

Try stop = temp.empty() instead. getline should not contain any new-line characters. An empty line should result in an empty string.

Also, Charles is correct, your while condition is incorrect, use while( !stop && ind < 20). The way you have it written the user needs to enter 20 values, and an empty line. Charles' change says to break when either condition is met (not both).

For the sake of completeness, here's the proposed new code:

bool stop = false;
int ind = 0;
while( !stop && ind < 20 ){

    cout << "Enter name #" << (ind+1) << ":";
    string temp;
    getline(cin, temp);
    if(temp.empty()) {
        stop = true;
    } else {
        names[ind] = temp;
    }

    ind++;    
}

Personally, I would write the code as follows:

vector<string> names;
for(int ind = 0; ind < 20; ind++) {
  cout << "Enter name #" << (ind + 1) << " (blank to stop): ";
  string name;
  getline(cin, name);
  if(name.empty() || cin.eof()) {
     break;
  }
  names.push_back(name);
}

cout << "Read " << names.length() << " names before empty line detected." << endl;
不回头走下去 2024-09-07 19:14:54

您想使用 cin.get();辛>>温度;我确实相信。

You want to use cin.get(); cin >> temp; I do believe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文