Problem with cin.getline();

Discussion in 'C++' started by Poof, Jul 11, 2003.

  1. Poof

    Poof Guest

    This is part of a database-kind-of program.

    char name[50];
    int age;

    for(int x = 0; x < 2; x++)
    {
    cout << "Enter your name : ";
    cin.getline(name, 50);
    cout << "Enter your age : ";
    cin >> age;
    }

    It runs fine the first time round the loop, but during its second run, it'll
    skip the cin.getline part and go straight to "Enter your age : ";

    So the output for the second run becomes
    "Enter your name : Enter your age : "

    It works fine if I use cin >> instead of cin.getline().... Anyone has any
    idea?

    Any responses are greatly appreciated!
     
    Poof, Jul 11, 2003
    #1
    1. Advertising

  2. Poof

    Poof Guest

    Yikes really sorry here... Found out that you have to add a cin.ignore() to
    clear the buffer
     
    Poof, Jul 11, 2003
    #2
    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. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,706
    Alex Vinokur
    Mar 6, 2005
  2. Markus

    cin.getline() problem

    Markus, Sep 1, 2006, in forum: C++
    Replies:
    11
    Views:
    804
  3. Chris
    Replies:
    7
    Views:
    445
    Chris
    Oct 20, 2006
  4. Devendra_Vidhale

    newbie problem with cin and getline

    Devendra_Vidhale, Jul 17, 2007, in forum: C++
    Replies:
    20
    Views:
    1,156
    James Kanze
    Jul 19, 2007
  5. bintom

    Problem with cin.getline()

    bintom, May 29, 2010, in forum: C++
    Replies:
    4
    Views:
    1,336
Loading...

Share This Page