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

  • SEARCH
  • Home
  • 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 6790065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:40:51+00:00 2026-05-26T17:40:51+00:00

I am helping someone with C++ input even though I don’t know C++ myself.

  • 0

I am helping someone with C++ input even though I don’t know C++ myself. Here’s a short example:

#include <iostream>
#include <string>

using namespace std;

int main() {
    int i;
    string s;

    cout << "enter i:\n";
    cin >> i;
    cout << "enter s:\n";
    getline(cin, s);
    //cin.ignore(100, '\n');
    cout << "i: " << i << ", s: " << s << "\n";
}

As expected, the getline() call will not prompt the user for input because it will simply swallow the trailing \n after the cin >> i call and return immediately. What I find odd is that if you uncomment the cin.ignore() call, the getline() call will prompt the user for input (even though nothing will be saved into s). Could someone explain why? In my view, the line in question should not change the behaviour at all.

  • 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-26T17:40:52+00:00Added an answer on May 26, 2026 at 5:40 pm

    Well, no. It is not the case that “getline() will prompt the user for input”. Clearly the ignore statement comes after the getline() one.

    What happens is that the ignore() statement simply blocks until it fulfils its task, i.e. gobble up 100 characters or a newline, whichever comes first (but mind the input buffering).

    As a general, though not directly related, piece of advice: don’t mix token extraction (>>) and getline(), for exactly this problem concerning newlines. Much better to just stick to one thing; preferably line reading so you can deal with errors and repeat the prompt. Also consider any unchecked read operation (>>, getline(), or istream::read()) a programming error; there should be conditionals surrounding all of those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am helping someone out with a javascript-based web app (even though I know
I am a java/php developer helping someone with actionscript. I don't understand why this
Is someone able to assist in helping understand how to get this C# example
I'm helping someone with a Wordpress blog which at one time was at http://example.com/example/
I have been helping someone debug some code where the error message was Day
Does someone have know how to trap clicks on Links in the WPF WebBrowser
Ok, so I am helping someone with their site. Their original coder thought it
I was helping someone with their homework and ran into this strange issue. The
Am helping someone to install WP on their current host, which unfortunately only has
I ma helping someone out to make a web site but I cant find

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.