Question

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


}
Was it helpful?

Solution

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

OTHER TIPS

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.

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

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;

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top