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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:46:53+00:00 2026-05-10T19:46:53+00:00

I’m learning C++ and writing little programs as I go along. The following is

  • 0

I’m learning C++ and writing little programs as I go along. The following is one such program:

// This program is intended to take any integer and convert to the // corresponding signed char.  #include <iostream>  int main() {   signed char sch = 0;   int n = 0;   while(true){     std::cin >> n;     sch = n;     std::cout << n << ' --> ' << sch << std::endl;   } } 

When I run this program and keep inputs at reasonably small absolute values, it behaves as expected. But when I enter larger inputs, e.g., 10000000000, the program repetitively spits out the same output. Some combinations of input cause erratic behavior. For example:

#: ./int2ch 10 10 -->   10000000000 10 -->  10 -->  10 -->  10 --> 

The program spits out ’10 –> ‘ until it’s killed. (With this particular sequence of inputs, the program’s output changes speed erratically.) I also noticed that the output of large values is determined by the previous legal input as well as the value of the current illegal input.

What’s going on? (I don’t care about fixing the program, that’s easy. I want to understand it.)

  • 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. 2026-05-10T19:46:53+00:00Added an answer on May 10, 2026 at 7:46 pm

    Basically your cin stream is in a fail state and thus returns immediately when you try to read it. Rewrite your example like this:

    #include <iostream>  int main() {   signed char sch = 0;   int n = 0;   while(std::cin >> n){     sch = n;     std::cout << n << ' --> ' << sch << std::endl;   } } 

    cin >> n will return a reference to cin, which you can test for ‘good-ness’ in a conditional. So basically the the ‘while(std::cin >> n)‘ is saying ‘while i could still read from standard input successfully, do the following’

    EDIT: the reason it repeatedly output the last good value entered is because that was the last value successfully read in n, the failed reads won’t change the value of n

    EDIT: as noted in a comment, you can clear the error state and try again something like this would probably work and just ignore bad numbers:

    #include <iostream> #include <climits>  int main() {     signed char sch = 0;     int n = 0;     while(true) {         if(std::cin >> n) {             sch = n;             std::cout << n << ' --> ' << sch << std::endl;         } else {             std::cin.clear(); // clear error state             std::cin.ignore(INT_MAX, '\n'); // ignore this line we couldn't read it         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 59k
  • Answers 59k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Call this template on the string you want to process:… May 11, 2026 at 8:58 am
  • added an answer The solution to this problem is well-known: mocking. Refactor your… May 11, 2026 at 8:58 am
  • added an answer For test_foo.c you simply need to tell the compiler where… May 11, 2026 at 8:58 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.