fetching two numbers separated by a whitespace (noob question)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnmcmadeup@gmail.com

    fetching two numbers separated by a whitespace (noob question)

    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;)?
  • Ian Collins

    #2
    Re: fetching two numbers separated by a whitespace (noob question)

    johnmcmadeup@gm ail.com 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.

    Comment

    • johnmcmadeup@gmail.com

      #3
      Re: fetching two numbers separated by a whitespace (noob question)

      On Apr 5, 11:32 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      johnmcmad...@gm ail.com 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.

      Comment

      • Jim Langston

        #4
        Re: fetching two numbers separated by a whitespace (noob question)

        johnmcmadeup@gm ail.com wrote:
        On Apr 5, 11:32 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        >johnmcmad...@g mail.com 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
        tazmaster@rocke tmail.com


        Comment

        Working...