C++ Detecting ENTER key pressed by user
- by user69514
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++;
}