Thread: getline

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    getline

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string name;
    
    	cout << "Enter a name" << endl;
    
    	getline(cin, name);
    
    	cout << "The name you entered is "
    		 << name << endl;
    
    	return 0;
    }
    Why do I have to press enter twice after I've entered a name? Is this something that's exclusive to Visual C++ 6.0?
    Last edited by volk; 05-05-2003 at 12:10 PM.

  2. #2
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Is getline(); recommended over cin.getline();, by the way?

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by volk
    Is getline(); recommended over cin.getline();, by the way?
    getline() is for strings and cin.getline() is for char arrays.

    as for the press enter twice thing, search the board. there is a fix for it for vc++ 6.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The hit return twice problem with getline() is a VC++ bug. You can read about it and get the fix at the MS website:

    http://support.microsoft.com/default...;EN-US;q240015

    Unfortunately, MS doesn't tell you what the file name is that contains the bug or where it's located. I hunted it down, and on my computer it's here:

    C:\Program Files\C++Microsoft Visual Studio\VC98\Include\String
    Last edited by 7stud; 05-05-2003 at 03:45 PM.

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    once again theres a better alternative than microsoft for info on their products.

    Fix to <string>
    The header <string> contains a definition for template function getline. It has a lookahead problem -- typing a delimiter to end the input doesn't return control until you type yet another character. Change the code as indicated by the comment:

    else if (_Tr::eq(_C, _D))
    {_Chg = true;
    _I.rdbuf()->snextc(); // replace snextc with sbumpc
    break; }
    via

    http://www.dinkumware.com/vc_fixes.html

  6. #6
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Why should I put up with that crap? I think it's time I get a new compiler. *sigh* Figures...Microsoft products.

    And judging from the link RoD posted, there are way more bugs with VC++.

    Hmmm, Borland maybe...

    Hmmm, I hear Comeau is the best...

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    RoD,

    I'm having a problem with getline() in the simple code below, and I was wondering if you could take a look at it. I am trying to read the first 5 char's from each line of an input file that looks like this:

    david55555555555
    sally66666666666666

    but, my output is:

    david

    I can't figure out what's going on with getline() or the file pointer, so I put a tellg() statement in after the first read of 5 char's, and I get tellg() = -1???
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    { 
    	char text[6];
    	char word[6];
    	
    	ifstream inFile("C:\\TestData\\input.txt");
    	
    	inFile.getline(text, 6);
    
    	cout<<inFile.tellg()<<endl;
    	
    	//discard the rest of the char's on the line
    	inFile.ignore(1000, '\n');
    	
    	//read the first 5 char's on the next line
    	inFile.getline(word, 6);
    	
    	cout<<text<<endl
    		<<word<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 05-05-2003 at 07:28 PM.

  8. #8
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    im not that deep into it but i'll have a look at it.

  9. #9
    JLBShecky
    Guest
    I went and tried source code out on my computer. I ran into the same problems as you described. I tried using seekg(), to reset the file pointer after 'File.getline(text, 6)' was called. When tellg() was called it still returned '-1', thus I figured that the problem was most likely being caused by the getline function call. I then went and checked MSDN to see if they said anything about the subject, in my brief search I did not find anything was of use. I then tried replacing 'getline' with 'get', and it properly displayed the desired data. Here is a modified version of your code that should work.

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    { 
    	char text[6];
    	char word[6];
    	
    	ifstream inFile("input.txt");
    	
    	inFile.get(text, 6);
    	
    	cout<<inFile.tellg()<<endl;
    	
    	//discard the rest of the char's on the line
    	inFile.ignore(1000, '\n');
    	
    	//read the first 5 char's on the next line
    	inFile.get(word, 6);
    	
    
    	cout<<text<<endl
    		<<word<<endl;
    
    	return 0;
    }
    I hope that the modified code works as well on you computer as it did on mine.

    -JLBShecky

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Thanks for the response. However, I already know get() works. I'm trying to find out why getline() won't work. It's a problem I came across when I advised a poster to use getline(), but the person couldn't get it to work, so I tried it, and much to my surprise it wouldn't work for me either. I have VC++ 6.0 as my compiler, and someone else ran it on a different compiler and said it worked.

  11. #11
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    hmm....maybe we should bring it to the attention of Dinkum and see if its a bug?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM