Thread: How does cin actually work

  1. #1
    Registered User blueCheese's Avatar
    Join Date
    Apr 2004

    Question How does cin actually work

    How does the cin operator actually work? For example when you request for an integer variable such as

    cin >> a;

    then the user enters 153. What actually happens when the user presses enter? I'm not looking for "a is initizaled to 153."

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Use your debugger and "step into".
    Or read this.


  3. #3
    Registered User
    Join Date
    Mar 2002
    I love to speculate, so here goes. If nothing else it might spur additional elucidation/clarification/discussion.

    In the iostream header file cin is declared to be a global object of the istream class. As such it inherits a number of things, like the state variable, form the ios class. In addition, each istream object, including cin, also has a buffer, call it rdbuf and make it an array of type char with capacity of 256 for arguments sake, and a number of functions and operators declared, of which one probably looks something like this:

    istream:perator >>(int);

    Now say the user enters 123<enter> at the prompt as in the OP.

    First, the 1 is entered into rdbuf[0], the 2 is entered into rdbuf[1], the 3 is entered into rdbuf[2] and a newline char is entered into rdbuf[3]. I also suspect that a counter variable is incremented with each entry.

    In any event, once the keystrokes are entered into rdbuf, the compiler has already entered the appropriate lines of code for the operator as listed above. So the lines are executed. The input either succeeds or it fails depending on the actual input.

    Maybe the lines of code to do the dirty work goes something like this:

    Look at rdbuf[0]. if it is a whitespace char, ignore it and all other subsequent sequential whitespace chars until a non white space char is encountered or until all elements in rdbuf have been evaluated. If no non-whitespace char have been encountered before all elements are evaluated, flip the fail bit in the state variable.

    else, look at the first non-whitespace char. If it isn't a digit, go into a terminal loop and get the user very frustrated.

    else if the first non-whitespace char is a digit, convert it to an int, maybe by calling atoi(rdbuf[0]) and store it in the variable indicated. The variable now has the value of 1.

    Then look at next char in rdbuf. if it isn't a digit, stop input into the variable. Leave all remaining char in rdbuf, placing user in confused state because the next time they call a function/operator that relys on rdbuf they're screwed by the remains of the original input in rdbuf, unless the only thing left is a whitespace char, in which case the next call to >> will ignore it anyway, but the variable has been successfully assigned the value derived so far (that is, 1), and the fail bit probably isn't flipped, even though the input was in error if the terminating non-digit wasn't a white space char.

    else if the next char in rdbuf is a digit, convert it to an int and store it in a temp variable of type int. Change the value in the variable as follows:
    variable = variable * 10 + temp

    repeat the last two steps until a non-digit char has been encountered.

    To further play with the minds of any unsuspecting end user, if the value of the variable exceeded 32500 (or some such upper limit for an int) at any point, then subtract 32500 from the value of the variable and add it to -32500 and continue.

    I'm sure it's really much more complicated than this. For example, the computer doesn't use base 10 for it's math. It uses base 2 and converts the results to base 10 so us mortals can actually read it without going crazy in the process. Likewise, rdbuf probably is a list of binary values, each group of eight of which represents the ASCII values of the keystrokes entered and not actually a char per se, but what the hey, who can understand that either. Very few of us probably even want to try.

    By now you're either laughing your as* off or think I'm completely off my rocker, so I'll give up here and see what zingers come my way.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001

    Lightbulb For further reading, research, and study...

    My favorite language reference is Dinkumware. It's the only complete C++ reference that I've found (other than the language standard itself). It's rather technical and brief. It could use more examples. You can access it free online, or download the HTML version for about $20.

    You can download the actual ANSI C++ language standard in PDF format for about $20 also. As you might expect, it's even more "technical" and not so easy to understand for those of us who aren't real computer scientists. The 500-700 pages of non-hyperlinked PDF are difficult to navigate too. And, in some places, the C++ standard refers-back to the C standard. So you'll need a copy of that too (another ~ $20).

    Finally, you could download the source code for one of the open-source compilers and look the actual code. Of course, some of this compiler-code is going to be written in assembly... Because you can't "compile the compiler" until you have a compiler!

  5. #5
    Registered User
    Join Date
    Mar 2004
    Quote Originally Posted by DougDbug
    Because you can't "compile the compiler" until you have a compiler!
    just out of curiosity but when u write a new language u dont have a compiler so the 1st version of the compiler u create has to compile itself so u can get a working compiler. i might have picked this up wrongly or misunderstood it but my teacher said something along those lines lol.

  6. #6
    Join Date
    Feb 2003
    actually, you compile the compiler by using a current language to compile it. for example, if you wanted to program up a new language, most of the time you can get away with writing up the compiler in C/C++ given that your computer already has a compiler for C/C++ on it. C and C++ compilers are usually written at the core in assembly, as when you start a new OS, assembly is pretty much the only language you have to work with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check if a cin is null
    By HumbuckeR in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2006, 08:16 AM
  2. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  3. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 11:53 AM
  4. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM
  5. Developers Wanted
    By Quasicom in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 08-24-2005, 12:46 AM