Thread: getline problem

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    getline problem

    I am currently working on a progect and I can figure somthing out. I wasnt going to post the mass amout of code I have so I wrote a small sample program with the same problem. Here is the code
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	string name;
    	char continueIt = 'Y';
    	while(continueIt == 'Y')
    	{
    		cout << "Enter Name: ";
    		getline(cin, name);
    		cout << name;
    		cout << "\nContinue(Y/N): ";
    		cin >> continueIt;
    	}
    
    }
    My problem is that the second iteration of the loop it skips over the getline and does not let me enter a new name.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    I thought getline(cin, name) was phrased as cin.getline(parm1,parm2,opt parm3);

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Im not sure, the way I have it works for the first iteration but none after that. Am I missing a parameter that would stop this?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This is a problem cause by mixing calls to getline with cin. It basically boils down to cin not extracting the trailing newline character from the input stream and then the next time getline gets called, it reads the newline left in the input stream and thinks the user entered an empty string (thinks the user pressed the "enter" key at that point). An easy solution is to call cin.ignore(); after the cin >> continueIt; call.

    [edit]Also, if you happen to be using the MSVC 5/6 compiler, you may also want to make sure you apply the fixes to the known issues in the string header file by looking here: http://www.dinkumware.com/vc_fixes.html [/edit]
    Last edited by hk_mp5kpdw; 09-22-2005 at 01:46 PM.
    "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

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    3
    Nice, it worked. Thanks.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by FoodDude
    I thought getline(cin, name) was phrased as cin.getline(parm1,parm2,opt parm3);
    There are different versions of the getline function. One, the one you are thinking of, is a member function of cin stream object and has the form you've shown above. The other version of the getline function is the one described in the string header file that is designed to work with string containers and can be used as TheCaptain has. That version also accepts an optional third parameter which has the same purpose as the optional parameter in the version you are thinking of.
    "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

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. getline() problem
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2005, 01:16 AM
  4. Need help with structures
    By yoursport in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2005, 11:59 AM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM