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 8892449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:00:01+00:00 2026-06-14T23:00:01+00:00

For some reason in my program when I reach a certain spot, I have

  • 0

For some reason in my program when I reach a certain spot, I have to press Enter twice in order to get it to submit. I added the clear to keep it from skipping input and the ignore() to keep it from keeping any extra characters in the buffer. I enter my input and then it drops down to a new line, I hit Enter again and it enter the input and continues the program no problem but I’m wondering why. Here’s a code snippet:

    cin.ignore();
    cout << "Enter Student Major (ex. COSC): ";
    cin.getline(student.major, 6);

    for(int i = 0; i < sizeof(student.major); i++)
        student.major[i] = toupper(student.major[i]);

    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

Any suggestions?

  • 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-06-14T23:00:02+00:00Added an answer on June 14, 2026 at 11:00 pm

    It seems to me that you are tossing too many cin.ignore() around, not knowing exactly why they are needed and when to put them there.

    There are two common circumstances where cin.ignore() is needed to “make input work right”:

    1. when mixing formatted and unformatted input;
    2. to recover from a formatted input error.

    In both cases, you want to get rid of spurious characters from the input buffer; if there isn’t any such character (which is probably what happens in your program), cin.ignore() will pause the execution and wait for user input – after all, you asked it to ignore some characters, and dammit, it will obey to its orders.

    (although ignore() by default would “eat” just one character, whatever it may be, the execution is paused until a newline is found because by default cin is line buffered – new input is not examined until a newline is recieved)

    Case 1:

    cin.ignore() calls are often needed if you are performing an unformatted input operation (like getline) after performing a formatted input operation (i.e. using the >> operator).

    This happens because the >> operator leaves the newline in the input buffer; that’s not a problem if you are performing only formatted input operations (by default they skip all the whitespace before trying to interpret the input), but it’s a problem if afterwards you do unformatted input: getline by default reads until it finds a newline, so the “spurious newline” left will make it stop reading immediately.

    So, here you will usually call cin.ignore(...) call to get rid of the newline just after the last formatted input operation you do in a row, guaranteeing that the input buffer is empty. Afterwards, you can call getline directly without fear, knowing that you left the buffer empty.

    It’s a bad idea, instead, to put it before any getline, as you seem to do in your code, since there may be code paths that lead to that getline that have the input buffer clean, so the ignore call will block.

    Case 2:

    when istream encounters an error in a formatted input operations, it leaves the “bad” characters in the buffer, so if you retry the operation you get stuck endlessly, since the offenders are still there. The usual clear()/ignore() idiom comes to the rescue, removing the whole offending line from the input buffer.

    Again, you don’t put the clear()/ignore() sequence at random, but only after you get an input error from a formatted input operation (which sets the failbit of the stream).


    Now, aside from these cases, it’s uncommon to use cin.ignore() (unless you actually want to skip characters); don’t spread it around randomly “just to be safe”, because otherwise you will encounter the problem you described.

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

Sidebar

Related Questions

I have a following program but for some reason it is throwing an error
I have a CSS file being defined in my program and for some reason
In my program, I'm grep-ing via NSTask. For some reason, sometimes I would get
For some reason, I can't get this program to work. I've had other CS
For some reason even though I have declared a string in Program.cs static and
This should be pretty simple but for some reason, I can't get this program
for some reason when running a program it refuses to enter a for loop,
For some reason this program is saying that 'switch' is not defined. What is
For some reason the following doesn't crash like my program does, but I'm pretty
For some reason, dialogue boxes don't close in my program, even though shown by

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.