Thread: getline() problem

  1. #1
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483

    getline() problem

    I've been having problems with getline lately everytime i use getline it just skips over it...
    Code:
    CODE:
    string name;
    int age;
    string About;
    cout << "Your name: ";
    getline(cine, name, '\n');
    cout << "Age: ";
    cin >> age;
    cout << "About Yourself: ";
    getline(cin, About, '\n');
    
    OUTPUT:
    Your name: Martin Bakiev
    Age: 14
    About Yourself: Press any key to continue...
    Whats wrong with it!

  2. #2

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>Whats wrong with it!

    Nothing. It's acting like a good getline() should. The "problem" is calling >> before you call getline(). The call to >> frequently leaves a newline char in the input buffer when it's done, which is what it is supposed to do, too, by the way. The newline char is frequently the terminating char for getline(). Thus after a call to >> when you call getline() the first char it sees in the input buffer is newline, so it "skips" over the variable and the program continues on. You can outflank this irritating combination in several ways, but the easiest is to call ignore() after >> and before getline(). We can debate the best set of parmaters to use in ignore, but that's the most common "fix". If you search the boards you'll find this problem discussed often.
    You're only born perfect.

  4. #4
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Hey it happened to me too!.
    When I mixed up cin>> and cin.getline() in the same program....

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    41
    getline(cine, name, '\n'); shouldent cine be cin
    Shouldent NULL be, 78, 85, 76, 76, or just 0 or, 4E, 55, 4C, 4C

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. Need help with structures
    By yoursport in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2005, 11:59 AM
  4. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM