help with cin.getline()

This is a discussion on help with cin.getline() within the C++ Programming forums, part of the General Programming Boards category; Hi there good people. Ok i'm puzzled, i got his as my main(), but I defined other functions b4 (just ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    help with cin.getline()

    Hi there good people.
    Ok i'm puzzled, i got his as my main(), but I defined other functions b4 (just for the record, since believe that they have no influence whatsoever in what's happening)

    int main()
    {
    int escolha;
    char txt[1000];
    cout<<"O APONTADOR DE HISTORIAS"<<endl;
    menuprint();
    cin>>escolha;
    if (escolha == 1)
    {
    cin.getline(txt, 1000, '\n');
    //does some other stuff.....
    return 0;
    }
    }
    my problem is the program doesn't stop at cin.getline(....) as spouzed, and read the input. This looks pretty straight forward to me, that's why it puzzles me even more. All the #includes are present.
    So if somebody could please clear me out on this matter, i would be ever so much appreciated.

    thankxx in advance for your kind attention.
    1976
    PS - in case you're wondering what language is in between quotes it's Portuguese

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The statement -

    cin>>escolha;

    is leaving a '\n' in the buffer which is then being picked up by getline causing it to terminate immeadiately. If you put

    cin.ignore(80,'\n');

    after cin>>escolha; then it will remove any stray input and the newline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.getline help
    By Cool Dude 2k in forum C Programming
    Replies: 2
    Last Post: 07-27-2005, 07:55 PM
  2. cin.getline and msgrcv
    By osal in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2005, 12:01 PM
  3. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  4. problem with cin.getline()
    By Waldo2k2 in forum C++ Programming
    Replies: 8
    Last Post: 05-28-2002, 06:53 PM

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