C++ Detecting ENTER key pressed by user

我们两清 提交于 2019-12-23 04:27:03

问题


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++;


}

回答1:


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



回答2:


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.




回答3:


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



回答4:


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;



回答5:


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



来源:https://stackoverflow.com/questions/2803502/c-detecting-enter-key-pressed-by-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!