Cin

This is a discussion on Cin within the C++ Programming forums, part of the General Programming Boards category; When you cascade cin such as cin >> var1 >> var2; it can recognize the two values when the user ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    38

    Cin

    When you cascade cin such as

    cin >> var1 >> var2;

    it can recognize the two values when the user presses enter or space...
    eg.
    Enter 2 numbers: 4 5
    or:
    Enter 2 numbers: 4
    5

    when i try to put anything else in there it outputs the value weird...

    eg. Enter 2 numbers: 4,5
    or Enter 2 numbers: 4 t 5

    why can the compiler only distinguish between spaces and return chars and why?
    #include <Jesus.h>
    It will save your life

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    because operator >> sees anything other than whitespace as an input and if its not the expected type of input sets the failbit. A comma and a lower case t are both not whitespace so they will not be disregarded.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    also, look into the declaration type of var1 and var2, if they are ints, the ',' and 't' will be stored as ascii.

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "why can the compiler only distinguish between spaces and return chars and why?"

    Create a class and define the >> operator any way you want if you're so unhappy with it.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    So if the >> operator doesn't see whitespace as input then wouldn't it connect the 2 values

    cin >> var1 >> var2

    as one number? does it do anything when it encounters whitespace?

    also when i put a char infront of a int value then output it, it is a different number. On my compiler any char infront of any number outputs to be the the same number every time. Why is this? (that eg input y45 otuputs to be eg. 6)?
    #include <Jesus.h>
    It will save your life

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    Mostly likely because the program takes the ascii number associated with the character. Your best bet would be to use cin.get in combination to isdigit to achieve whatever you are trying to get to here. Unless you wish to overload the >> operator (always fun to so ). If you want me to demonstrate how to get numbers divided by a certain character just leave a post here and i will post some code..... Don't ask me to show you how to overload >> though... been to long since i have done that kinda stuff

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    whitespace before read item is ignored, whitespace after terminates that read.

    so ...

    cin>>a>>b>>c;

    ignore whitespace
    read a
    ignore whitespace
    read b
    ignore whitespace
    read c

    notice how anything left over from the reading of c is left in the buffer. This is typically \n but could be any sort of crap. Knowing this you should be able to tell me why this getline call appears to fail...

    int a,b,c;
    char* d = new char[256];
    cin>>a>>b>>c;
    cin.getline(d,256);
    delete[] d;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 12:14 PM
  2. Check if a cin is null
    By HumbuckeR in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2006, 09:16 AM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  4. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 03:57 PM
  5. multiple instances of cin
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2004, 05:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21