Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 777197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:33:59+00:00 2026-05-14T19:33:59+00:00

I have a loop where I ask the user to enter a name. I

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:34:00+00:00Added an answer on May 14, 2026 at 7:34 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to heat up an android phone (don't ask...) So far I tried
I have the following (part of big ksh script) while [[ $? -ne 0
I have written a TCPIP server that implements a FileSystemWatcher and fills a queue
I have created a rather complicated piece of (Java) code for a game I
This is the code i have at the moment print Please input the filename:
i have a problem with the order of execution of the threads created consecutively.
I would to set it up where if someone sends in a request logout
If a browser requests http://site.com/?q=abc and gets a 301 redirect response to location http://site.com/?q=ABC
Let me explain what I mean in my title. Let's say, that for example
A server offers a SOAP function call interface. Hundreds, perhaps a few thousand, of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.