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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:29:44+00:00 2026-06-18T08:29:44+00:00

I am a beginner programmer learning c++. I am having a nagging issue with

  • 0

I am a beginner programmer learning c++. I am having a nagging issue with the cin command.
In the program section below, if I enter a wrong type at the 1st cin command, the program will not execute any of the following cin commands at all, but will execute the rest of the program.

//start
#include <iostream>
using namespace std;
int main()
{
    int x=0;
    cout << endl << "Enter an integer" << endl;

    //enter integer here. If wrong type is entered, goes to else
    if (cin >> x){
            cout << "The value is " << x << endl;
    }
    else {
         cout << "You made a mistake" << endl; //executes
         cin.ignore();
         cin.clear();
    }
    cout << "Check 1" << endl; //executes
    cin >> x;                  //skips
    cout << "Check 2" << endl; //executes
    cin >> x;                  //skips
    return 0;                  
}
//end

Instead of the if else, if i put the same concept in a loop
while (!(cin >> x))
the program goes into an infinite loop upon enterring a wrong input.
Please help me explain this phenomenon, as the text book i am following says the code typed above should work as intended.

Thank you

  • 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-18T08:29:46+00:00Added an answer on June 18, 2026 at 8:29 am

    cin is an input stream. If an error occurs cin goes into a let’s call it “error occured” state. While in this state no character input can be made, your request to collect a character from the input stream will be ignored. With clear() you clear the error and the input stream stops ignoring you.

    Here is the ignore function prototype

    istream&  ignore ( streamsize n = 1, int delim = EOF );
    

    This function gets characters from the input stream and discards them, but you can’t get any character if your stream is ignoring you, so you have to first clear() the stream then ignore() it.

    Also, a note on the side: If someone inputs, for example "abc", on the first input request your cin gets only one character that is 'a' and "bc" stays in the buffer waiting to be picked up, but the next call to cin gets the 'b' and 'c' stays in the buffer, so you again end up with an error.

    The problem with this example is that the cin.ignore() if no arguments are handed to it only ignores 1 character after you clear(). and the second cin gets 'c' so you still have a problem.

    A general solution to this problem would be to call

    cin.ignore(10000, '\n');
    

    The first number just has to be some huge number that you don’t expect someone would enter, I usually put in 10000.

    This call makes sure that you pick up all the characters from the false input or that you pick up every character before the enter was pressed so your input stream doesn’t get into the “error occurred” state twice.

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

Sidebar

Related Questions

I'm a beginner programmer, and I'm learning to program a web app using WampServer.
I'm a beginner programmer, and am wondering how to work around this issue. The
I'm a novice/beginner programmer having problems getting some simple client/server C code working. My
I'm a beginner programmer, and I've installed XAMPP with the intention of learning a
I'm a beginner programmer, I decided to write a simple program getting prime factors
Beginner programmer here. So bear with me. I have a simple python program. print
I'm a C++ programmer learning Ruby. In a beginner book I read: A class
I'm a young (highschool next year) beginner programmer, currently learning Python and starting to
i am a beginner level programmer and have just started learning programming in C++,
As a beginner programmer, just now learning the basics of OOP, I've run into

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.