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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:28:00+00:00 2026-05-23T12:28:00+00:00

I am new to C++ coding. I have noticed that if I enter multiple

  • 0

I am new to C++ coding.

I have noticed that if I enter multiple characters for a character variable then the test program given below goes into an infinite loop. I am not able to debug this issue properly. Can someone help me see how the flow of the program goes when such input is entered. I doubt about some unsafe conversions happening.

There is a possibility that the overflow values are taken as input for next integer variable, but I am checking for variable in a loop and asking the user to
re-enter a value is invalid. The program does not halt for taking new input for number. Instead it infinitely prints “Enter a Number”. Why is it that the cin is not working for
taking the new input? Is there some modification in this program that can rectify this error?

#include <iostream>

int main()
{
    char name;
    int number=-1;
    cout << "Enter a character: \n";
    cin >> name; //Use input like abc here
    cout << "number = " << number;
    while (number == -1)
    {
        cout << "Enter a number: \n";
        cin >> number; // This never waits for user input
    }
    return 0;
}


Output
Enter a character:
ytc
number = -1Enter a number:
Enter a number:
Enter a number:
  • 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-23T12:28:00+00:00Added an answer on May 23, 2026 at 12:28 pm

    When the user enters mutiple characters on the first query, only the first one is extracted and stored in name.

    After that, the loop is entered. The first character is read, and it is not a number, thus the stream goes into a bad state. All subsequent reads fail, so the value of number never changes, and you have an infinite loop.

    The following program will do what you want:

    #include <iostream>
    
    int main() {
      char name = '\0';
      int number = -1;
      std::cout << "Enter a character: " << std::endl;
      std::cin >> name;
      std::cout << "You entered " << name << std::endl;
      while(number == -1) {
        std::cout << "Enter a number: " << std::endl;
        if(!(std::cin >> number)) {
          if(std::cin.eof()) {
            std::cout << "User pressed CRTL+d" << std::endl;
            break;
          }
          std::cout << "This was not a valid number - try again: " << std::endl;
          std::cin.clear();
          std::cin.get(); // extract one character
        }
        else
          std::cout << "You entered " << number << std::endl;
      }
    }
    

    The condition if(!(std::cin >> number)) checks if the extraction was successful. If not, an error-message is printed, the stream-state is cleared, and one character extracted. The latter is done to prevent an infinite loop, because if we never extract the bad character, we will get the same error over and over again.

    Note that the extraction std::cin >> number will try to extract the longest string that represents a number. If the user inputs 234t23432, then it will extract 234, because the t is the first character that can not be interpreted as part of the number.

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

Sidebar

Related Questions

Hi I have a small problem. I am coding a program that displays the
I have noticed that when I was running Windows XP, if my code dereferenced
I am new to php and am asking for some coding help. I have
First i have declare that i am new to javascript. I have created a
I have a new web app that is packaged as a WAR as part
I have a project that adds elements to an AutoCad drawing. I noticed that
I am coming from a Spring/hibernate background. I have noticed that Rails has no
I'm a relatively new C programmer, and I've noticed that many conventions from other
I am new to the world of coding as well as CSS. I have
I have a new 1 TB drive coming in tomorrow. What is the best

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.