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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:18:54+00:00 2026-05-27T12:18:54+00:00

Here is a small contrived program which prints the integer just typed to the

  • 0

Here is a small contrived program which prints the integer just typed to the screen.

#include <iostream>
int main(int argc, char *argv[])
{
  int n;
  while(std::cin>>n)
    {
      std::cout<<"You typed "<<n<<std::endl;
    } 

  return 0;
}

Now when I enter integers the program works normally. However, if I enter floating point numbers like 10.8 the program displays 10 to the screen (as it should, since it is casting the floating point number to an integer), and then the program exits. Why is this happening? Given below is a sample terminal output.

Desktop: ./a.out
4
You typed 4
-9
You typed -9
10.8
You typed 10
Desktop: 
  • 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-27T12:18:55+00:00Added an answer on May 27, 2026 at 12:18 pm

    After “You typed 10” line, the characters “.8” remain in the stream. When it tries to read that into an integer, it fails, and so it puts the stream into a bad state.

    Since the stream is in a bad state, your loop condition fails and you fall out of the loop.

    (Specifically, it sets the “bad” bit, I think, but it’s been too long since I dealt with that and I’m not sure — check the documentation if you need that level of detail)

    Aside: be aware if you tried to read again, the characters “.8” would still remain in the stream, and you would fail again. This is a common I/O error.

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

Sidebar

Related Questions

Just a small SVN problem here. I setup my own SVN server Setting up
I have here a small question. I wrote a small multi threaded app which
Say I do this (a contrived example): #include <iostream> #include <fstream> using namespace std;
I'm having a small issue here: <div id=navbar> <ul id=navtabs class=floatcontainer> <li <?php if
I am stuck with a small problem here.. What i am trying to do
I've a small issue here. I am using an if statement with UIAlertView and
I'm getting crazy with a small problem here, I keep getting an error and
I want to do formatting using echo in shell scripting. Here is a small
Here is the situation: I am making a small prog to parse server log
Small design question here. I'm trying to develop a calculation app in C#. I

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.