C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
getline function Posted by jedi06 on 2 Apr 2006 at 12:41 PM
In the getline function you have to specify how many characters you want to extract. istream& getline (char* s, streamsize n, char delim );

To me that is dumb, becuase it is not going to actually get the line if it is larger than you expected. Is there a way to get the whole line no matter how large it is?
Report
Re: getline function Posted by jambeard on 2 Apr 2006 at 2:21 PM
This message was edited by jambeard at 2006-4-2 14:28:9

: In the getline function you have to specify how many characters you want to extract. istream& getline (char* s, streamsize n, char delim );
:
: To me that is dumb, becuase it is not going to actually get the line if it is larger than you expected. Is there a way to get the whole line no matter how large it is?
:


If I were you I'd sack off trying to use that, I found using the getline function loads of hassle!!

Try this:

string readLine(istream &in, string &s)
{
	string returnString;

	char c;

	s.erase();	// clear the old string

	c = in.get();		// read one character ahead
	while ( c!='\n' )
	{
		s = s + c;		// add the string
		c = in.get();	// read the next character
	}

	returnString = s;

	return returnString;
}


And use it like this:

int main()
{
    string lineOfText;    //Place to store the line of text

    readLine(cin, lineOfText);    //read the line

    cout << "You wrote " << lineOfText << endl;    //Output what the user entered

    return 0;
}


Hope it helps.

J


Report
Re: getline function Posted by Lundin on 2 Apr 2006 at 11:24 PM
: This message was edited by jambeard at 2006-4-2 14:28:9

: : In the getline function you have to specify how many characters you want to extract. istream& getline (char* s, streamsize n, char delim );
: :
: : To me that is dumb, becuase it is not going to actually get the line if it is larger than you expected. Is there a way to get the whole line no matter how large it is?
: :
:
:
: If I were you I'd sack off trying to use that, I found using the getline function loads of hassle!!
:
: Try this:
:
:
: string readLine(istream &in, string &s)
: {
: 	string returnString;
: 
: 	char c;
: 
: 	s.erase();	// clear the old string
: 
: 	c = in.get();		// read one character ahead
: 	while ( c!='\n' )
: 	{
: 		s = s + c;		// add the string
: 		c = in.get();	// read the next character
: 	}
: 
: 	returnString = s;
: 
: 	return returnString;
: }
: 

:
: And use it like this:
:
:
: int main()
: {
:     string lineOfText;    //Place to store the line of text
: 
:     readLine(cin, lineOfText);    //read the line
: 
:     cout << "You wrote " << lineOfText << endl;    //Output what the user entered
: 
:     return 0;
: }
: 

:
: Hope it helps.
:
: J
:
:
:


And for C, replace cin.get() with fgets().
Report
Re: getline function Posted by Federal101 on 6 Apr 2006 at 5:47 AM
: : This message was edited by jambeard at 2006-4-2 14:28:9

: : : In the getline function you have to specify how many characters you want to extract. istream& getline (char* s, streamsize n, char delim );
: : :
: : : To me that is dumb, becuase it is not going to actually get the line if it is larger than you expected. Is there a way to get the whole line no matter how large it is?
: : :
: :
: :
: : If I were you I'd sack off trying to use that, I found using the getline function loads of hassle!!
: :
: : Try this:
: :
: :
: : string readLine(istream &in, string &s)
: : {
: : 	string returnString;
: : 
: : 	char c;
: : 
: : 	s.erase();	// clear the old string
: : 
: : 	c = in.get();		// read one character ahead
: : 	while ( c!='\n' )
: : 	{
: : 		s = s + c;		// add the string
: : 		c = in.get();	// read the next character
: : 	}
: : 
: : 	returnString = s;
: : 
: : 	return returnString;
: : }
: : 

: :
: : And use it like this:
: :
: :
: : int main()
: : {
: :     string lineOfText;    //Place to store the line of text
: : 
: :     readLine(cin, lineOfText);    //read the line
: : 
: :     cout << "You wrote " << lineOfText << endl;    //Output what the user entered
: : 
: :     return 0;
: : }
: : 

: :
: : Hope it helps.
: :
: : J
: :
: :
: :
:
:
: And for C, replace cin.get() with fgets().
:
Or you could just use..
#include <string>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
	string input;
	cout << "Enter a string: ";
	getline( cin, input );
	cout << "The string is: " << input << endl;
	return 0;
}

Report
Re: getline function Posted by jambeard on 6 Apr 2006 at 9:47 AM

: Or you could just use..
:
: #include <string>
: #include <iostream>
: using namespace std;
: 
: int main(int argc, char* argv[])
: {
: 	string input;
: 	cout << "Enter a string: ";
: 	getline( cin, input );
: 	cout << "The string is: " << input << endl;
: 	return 0;
: }
: 

:

You could but wasn't there a bug in that (or a version of it)? I tried using it for a project only 3 months ago and had to give up and write the getline function myself.
Report
Re: getline function Posted by Federal101 on 6 Apr 2006 at 10:26 AM
:
: : Or you could just use..
: :
: : #include <string>
: : #include <iostream>
: : using namespace std;
: : 
: : int main(int argc, char* argv[])
: : {
: : 	string input;
: : 	cout << "Enter a string: ";
: : 	getline( cin, input );
: : 	cout << "The string is: " << input << endl;
: : 	return 0;
: : }
: : 

: :
:
: You could but wasn't there a bug in that (or a version of it)? I tried using it for a project only 3 months ago and had to give up and write the getline function myself.

VS 6.0 has the famous getline bug.

Here is the page that shows the fix

http://support.microsoft.com/default.aspx?scid=kb;en-us;240015

Once you make this change getline should work for you.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.