fetching two numbers separated by a whitespace (noob question)

Discussion in 'C++' started by johnmcmadeup@gmail.com, Apr 5, 2008.

  1. Guest

    How can I make it so that, if the user inputs 2 numbers with a space
    in between , I can get both numbers(instead of just the first number,
    which happens when I try cin>>var;)?
     
    , Apr 5, 2008
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > How can I make it so that, if the user inputs 2 numbers with a space
    > in between , I can get both numbers(instead of just the first number,
    > which happens when I try cin>>var;)?


    std::cin >> var1 >> var2;

    --
    Ian Collins.
     
    Ian Collins, Apr 5, 2008
    #2
    1. Advertising

  3. Guest

    On Apr 5, 11:32 pm, Ian Collins <> wrote:
    > wrote:
    > > How can I make it so that, if the user inputs 2 numbers with a space
    > > in between , I can get both numbers(instead of just the first number,
    > > which happens when I try cin>>var;)?

    >
    > std::cin >> var1 >> var2;
    >
    > --
    > Ian Collins.


    Thanks a lot. I always though that that would force the user to hit
    enter between the 2 numbers.
     
    , Apr 5, 2008
    #3
  4. Jim Langston Guest

    wrote:
    > On Apr 5, 11:32 pm, Ian Collins <> wrote:
    >> wrote:
    >>> How can I make it so that, if the user inputs 2 numbers with a space
    >>> in between , I can get both numbers(instead of just the first
    >>> number, which happens when I try cin>>var;)?

    >>
    >> std::cin >> var1 >> var2;

    >
    > Thanks a lot. I always though that that would force the user to hit
    > enter between the 2 numbers.


    Any whitespace will do, space, tab, enter. In fact if it's a number any non
    number will cause the first input to stop. This may cause the var2 to not
    get a value if it's not a number. I.E. If the user enters
    1x2
    var2 is undefined.

    Also, if you want all the numeric digits in one variable you could use a
    std::string and getline. I.E.

    std::string Line;
    std::getline( std::cin, Line );

    That will accept all characters types into Line until the user presses
    enter. But then it's a string, not a number.


    --
    Jim Langston
     
    Jim Langston, Apr 6, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oli Filth
    Replies:
    9
    Views:
    3,368
    Uncle Pirate
    Jan 17, 2005
  2. Peter Nilsson

    Re: Write two separated objects?

    Peter Nilsson, Apr 14, 2008, in forum: C Programming
    Replies:
    3
    Views:
    304
    Peter Nilsson
    Apr 14, 2008
  3. Replies:
    10
    Views:
    803
    Eric Brunel
    Dec 16, 2008
  4. MRAB
    Replies:
    3
    Views:
    409
  5. Replies:
    6
    Views:
    128
    Tad McClellan
    Jul 5, 2005
Loading...

Share This Page