Thread: Strange problem with GETLINE

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    48

    Strange problem with GETLINE

    I'm having a strange issue when reading in user inputs using getline()......

    I'm asking the user for several things: name, address, city, state, etc.
    The problem is, when the prompt first displays, it's skipping over the first input, i.e.:

    Full Name:
    Street Address: _ <---- (That being the cursor)

    Here's the code, maybe can see where I went wrong, or what the problem is, thanks.

    Code:
    		cout << endl << "Full Name          : ";
    		getline(cin, contact_name);
    		cout << endl << "Street Address     : ";
    		getline(cin, contact_address);
    		cout << endl << "City               : ";
    		getline(cin, contact_city);
    		cout << endl << "State              : ";
    		getline(cin, contact_state);
    		cout << endl << "Zip Code           : ";
    		getline(cin, contact_zip);
    		cout << endl << "Phone Number       : ";
    		getline(cin, contact_phone);
    		cout << endl;

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Hmmmmm. Concentrating.... concentrating.... Ahh yes.

    Using my extra sensory perception powers, I can see that you have a scanf() in your code prior to the getline.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    I'm not sure what you mean...but I don't use scanf() at all in my prog.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    The entire program is actually a list of a bunch of programs that the user can choose to operate through a menu. I just used a switch statement to hold the different programs, and this one is number 2 on the list. I'm not done working on it. Basically it displays a customer contact card. It reads in the name, address, etc. and displays it in a particular format. The code reads as:

    Code:
    	case '2':
    		system("cls");
    		cout << "***************************************************" << endl;
    		cout << "***** C U S T O M E R  C O N T A C T  C A R D *****" << endl;
    		cout << "***************************************************" << endl;
    
    		cout << endl << "First Name         : ";
    		getline(cin, contact_first);
    		cout << endl << "Last Name          : ";
    		getline(cin, contact_last);
    		cout << endl << "Street Address     : ";
    		getline(cin, contact_address);
    		cout << endl << "City               : ";
    		getline(cin, contact_city);
    		cout << endl << "State              : ";
    		getline(cin, contact_state);
    		cout << endl << "Zip Code           : ";
    		getline(cin, contact_zip);
    		cout << endl << "Phone Number       : ";
    		getline(cin, contact_phone);
    		cout << endl;
    		return(0);
    		break;
    This is just the part that reads in the info. But like I said, it is skipping over the first input. Still confused....

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Quote Originally Posted by wco5002 View Post
    it is skipping over the first input. Still confused....
    You need to post all the code that is getting used before this. I am going to say you used cin someplace before this bit of code. If you did search for mixing cin and getline and post back with any questions.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    I did use cin earlier in the program, but it was outside of the switch statement, so would it affect it?

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    Figured it out:

    Just used cin.clear(); and cin.sync();

    It seemed to do the trick.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Do you understand why you needed to do that?
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    It is because my last cout << endl; was stored in the buffer, which is what the first getline() was referencing?

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    Worded that a little weird, I was wondering if that is what the problem was.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    No, it's because you had a newline char left in the input buffer that cin left behind and getline() picked up. It has nothing to do with cout.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    Alright, thanks for the insight!

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by wco5002 View Post
    Figured it out:

    Just used cin.clear(); and cin.sync();

    It seemed to do the trick.
    FYI, the sync is not needed here.

    [edit]...and after seeing Daved's post below, I realize that I saw "clear" and for some reason was thinking "ignore". Yes, that should be ignore and not clear.[/edit]
    Last edited by hk_mp5kpdw; 07-07-2008 at 10:38 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Normally we use cin.ignore() to ignore the newline. I honestly don't know if sync is guaranteed to work, but adding cin.ignore() after all calls to cin >> generally does.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem
    By h3ro in forum Windows Programming
    Replies: 2
    Last Post: 09-02-2008, 12:14 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM