ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi thanks for the response snacky but i get an error when i try to use getline
//Seutp a few variables
string name;
string number;
string address;
//Get the information
cout << "Create a new entry\n" << endl;
cout << "Name > ";
getline(name);
cout << "Number > ";
getline(number);
cout << "Address > ";
getline(address);
and the error:
project1.cc:45: cannot convert `std::string' to `char**' for argument `1' to `
__ssize_t getline(char**, size_t*, FILE*)'
project1.cc:47: cannot convert `std::string' to `char**' for argument `1' to `
__ssize_t getline(char**, size_t*, FILE*)'
project1.cc:49: cannot convert `std::string' to `char**' for argument `1' to `
__ssize_t getline(char**, size_t*, FILE*)'
Oops... posted at the same time. Just tried this and it worked. Must be one of the template functions in STL? Never used this before. It's good to learn something new every once in awhile, maybe I should look at some of these functions a bit more. I've only ever really used a few of them.
Have you used cin previously to read in any other variables? Often times if you have code like so:
Code:
string s1, s2;
cin >> s1;
getline(cin, s2);
When it reads in s1, the LF (or CRLF pair) is still in the input buffer, so the getline() right afterwords reads up to THAT point, which often would result in an empty string....
Not sure I'm explaining myself very well, so as an example, for that first cin if you were to enter something like so:
myinput<return>
The first cin reads myinput into s1, leaving the <return> character, so that when the readline gets called, it finds that right away, so it reads a blank string into s2....
No, that's not quite what I was saying to do. I was asking if you use cin that way anyplace before you do the getline, and trying to explain why that could cause a problem. It kind of seems like you might be, and if so, you could do a getline() after that in order to clear the return from the buffer.... So for example if you are doing something like so:
Code:
int n;
string name;
cout << "Enter number of entries: ";
cin >> n;
for (int c=0;c<n;c++)
{
cout << "Enter name " << c << ": ";
getline(cin, name);
cout << "You entered \"" << name << "\"" << endl;
}
Enter number of entries: 3<return>
Enter name 0:
You entered ""
Enter name 1: This should be name 0?<return>
You entered "This should be name 0?"
Enter name 2: What is going on?<return>
You entered "What is going on?"
The stuff in bold is what you would have entered, the <return> is to show where you would have hit the Enter key...
What happened is that when you entered 3, cin << n ONLY read in the 3 and left the <return> in the input buffer.
So... when the next input used a getline, it returned right away because it read the <return> that was left over from entering the 3<return>....
To fix this, you could do something like this instead:
Code:
int n;
string name;
string temp;
cout << "Enter number of entries: ";
cin >> n;
getline(cin, temp); // Get rid of extraneous line feed
for (int c=0;c<n;c++)
{
cout << "Enter name " << c << ": ";
getline(cin, name);
cout << "You entered \"" << name << "\"" << endl;
}
Hmmm... ok.. that code should work if you aren't using cin like that anywhere before it is called. That means not just in the same scope as that code, but anywhere. Even if you had that code in a function, and had a cin >> something elsewhere before that function was called, you would run into that problem...
But, if you don't have anything like that ANYWHERE, I'm out of ideas of what might be wrong.
From MSDN's documentation of basic_istream::ignore()
Code:
Causes a number of elements to be skipped from the current read position.
basic_istream& ignore(
streamsize _Count = 1,
int_type _Delim = traits_type::eof( )
);
Parameters
_Count
The number of elements to skip from the current read position.
_Delim
The element that, if encountered before count, causes ignore to return and
allowing all elements after _Delim to be read.
Return Value
The stream (*this).
Remarks
The unformatted input function extracts up to _Count elements and
discards them. If _Count equals numeric_limits<int>::max, however, it is
taken as arbitrarily large. Extraction stops early on end of file or on an
element _Ch such that traits_type::to_int_type(_Ch) compares equal to
_Delim (which is also extracted). The function returns *this.
So, if cin.ignore() is working, it further supports my theory that you have read something in prior to your getline, since it is skipping past a character. (Likely a linefeed character.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.