cin.getline & cin>> interference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vikram

    #1

    cin.getline & cin>> interference

    Hi,

    I don't remember if it happened previously, but nowadays
    I'm having problem with using cin.getline function and
    cin>> function simultaneously.

    I have Visual Studio 6. If I use cin.getline function at
    first and then I use the cin>> function then the next time
    cin.getline function does not work.

    Eg:-

    cout<<"a::";
    cin.getline(a, 20);
    cout<<"Num::";
    cin>>Num;
    cout<<"b::";
    cin.getline(b,2 0);
    cout<<"c::";
    cin.getline(c, 20);

    Now in output it will input for 'a' then 'Num' but it will
    leave input for 'b' and then it will input for 'c'.

    Please do help me with this problem.

    Thanks,
    Vikram.
  • Peter van der Goes

    #2
    Re: cin.getline &amp; cin&gt;&gt; interference


    "Vikram" <vikram@softkee .com> wrote in message
    news:057401c3cf 69$37f8e500$a60 1280a@phx.gbl.. .[color=blue]
    > Hi,
    >
    > I don't remember if it happened previously, but nowadays
    > I'm having problem with using cin.getline function and
    > cin>> function simultaneously.
    >
    > I have Visual Studio 6. If I use cin.getline function at
    > first and then I use the cin>> function then the next time
    > cin.getline function does not work.
    >
    > Eg:-
    >
    > cout<<"a::";
    > cin.getline(a, 20);
    > cout<<"Num::";
    > cin>>Num;
    > cout<<"b::";
    > cin.getline(b,2 0);
    > cout<<"c::";
    > cin.getline(c, 20);
    >
    > Now in output it will input for 'a' then 'Num' but it will
    > leave input for 'b' and then it will input for 'c'.
    >
    > Please do help me with this problem.
    >
    > Thanks,
    > Vikram.[/color]

    That's because cin >> leaves the return character in the input buffer, which
    is then picked up by cin.getline().
    Check out cin.ignore(100, '\n') to use between the cin >>'s and
    cin.getline()'s .

    --
    Peter [MVP Academic]


    Comment

    • Peter van der Goes

      #3
      Re: cin.getline &amp; cin&gt;&gt; interference


      "Vikram" <vikram@softkee .com> wrote in message
      news:057401c3cf 69$37f8e500$a60 1280a@phx.gbl.. .[color=blue]
      > Hi,
      >
      > I don't remember if it happened previously, but nowadays
      > I'm having problem with using cin.getline function and
      > cin>> function simultaneously.
      >
      > I have Visual Studio 6. If I use cin.getline function at
      > first and then I use the cin>> function then the next time
      > cin.getline function does not work.
      >
      > Eg:-
      >
      > cout<<"a::";
      > cin.getline(a, 20);
      > cout<<"Num::";
      > cin>>Num;
      > cout<<"b::";
      > cin.getline(b,2 0);
      > cout<<"c::";
      > cin.getline(c, 20);
      >
      > Now in output it will input for 'a' then 'Num' but it will
      > leave input for 'b' and then it will input for 'c'.
      >
      > Please do help me with this problem.
      >
      > Thanks,
      > Vikram.[/color]

      That's because cin >> leaves the return character in the input buffer, which
      is then picked up by cin.getline().
      Check out cin.ignore(100, '\n') to use between the cin >>'s and
      cin.getline()'s .

      --
      Peter [MVP Academic]


      Comment

      Working...