Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

std::getline doesn't work

Status
Not open for further replies.

Kirilla

Programmer
Jul 12, 2000
101
HU
Hi,

I've problem with the Standard Library's getline function.
As I know, it requires 3 params:
1. an input stream
2. a string objet
3. a const char
...
std::getline( cin, mystring, '\n');
...

I t doesn't work at me. When I run my code and I debug this line, after I stepped over mystring object is still empty.

What is wrong?

thanks,

Kirilla
 
Try this sample code to see if it works:
Code:
#pragma warning(disable:4786)
#include <string>
#include <iostream>

using namespace std ;

void main()
{
    string s1;
    cout << &quot;Enter a sentence&quot;<<endl;
    getline(cin,s1,'\n');
    cout << &quot;You entered: &quot; << s1;
}
 
Hi,

Thanks for your answer. I know the usage of getline, but I don't know, why it doesn't work.

I've try to explain what is the problem. See the following code:


...
string s1, s2;
cout << &quot;Enter a sentence&quot;<<endl;
getline(cin,s1,'\n');
getline(cin,s2,'\n');
cout << &quot;You entered: &quot; << s1 << endl;
cout << &quot;You entered: &quot; << s2 << endl;
...


the result is:

Enter a sentence
This is a
sentence.
You entered:
You entered: This is a


As you can see s1 is empty and s2 get the first part of getline ( &quot;This is a &quot;). So, that is I don't understand.

Your sample's result was :

Enter a sentence
This is a sentence.
You entered:


best regards,

Kirilla
 
I tested BOTH your codes and it WORKS for me. The only thing is that you need to enter an extra line before the first two lines are &quot;read in&quot;. Let me know if the below code gets you the same result

Sample output for Kirilla's code:
----------------------------------
Enter a sentence:
This is first line
This is second line
This is third line

You entered This is first line
You entered This is second line
-----------------------------------

The code that produced the result (I used Visual C++ 6.0 compiler)

-----------------------------------------
#include <string>
#include <iostream>

using namespace std ;

void main()
{
string s1, s2, s3;
cout << &quot;Enter a sentence \n&quot;;
getline(cin,s1,'\n');
getline(cin,s2,'\n');
cout << &quot;You entered: &quot; << s1 << endl;
cout << &quot;You entered: &quot; << s2 << endl;

// to stop the output screen
getline(cin,s3,'\n');

}
----------------------------------------------
 
Hi,

Thank you. There was problem with other part of my code. I've tried your sample in a new simple console project and it was working. I had to modify the <string> header file and there is no extra line.

see details at MSDN:
BUG: getline Template Function Reads Extra Character
Q240015

best regards,

Kirilla
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top