Thread: help with cin.getline()

  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
    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, 06: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, 05:53 PM