Hello

Using :

cout << "enter 5 letter";
getline(cin, letter);

executes fine but when running PC- Lint:
getline(cin, expectedLabel): error 534: (Warning -- Ignoring return value of function 'std::getline(std::basic_istream<char,std::char_traits<char>> &, std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)'
error 830: (Info -- Location cited in prior message)

I was wondering if anyone could tell me the problem with getline and is there an alternitve i do wish to enter 5 letters

Recommended Answers

All 5 Replies

Any idea why it is actually bringing up this warning ? and why ?

Thanks

It gets a little convoluted... getline is a global function that mixes strings and streams together, since some programmer was too lazy to make char arrays to use the stream.getline function.

To answer your question, I hope... It is because getline returns the stream parameter that you passed to it. In your case, the cin stream. This may seem strange, but they do have their reasons. The biggest of which is to chain operations. The most common example is cin >> a >> b >> c >> etc; (If you don't know how those work, it's pretty simple. >> is just an ordinary function except for the fact that it changes the way you invoke them. Instead of (cin.get(a)).get(b) you can simply type cin >> a >> b). So because it returns the stream and you're not doing anything with that stream it's getting confused. Most compilers know better than to complain about this though.

There are other reasons why they do this, too.
while(getline(filestream, stringname)) {}
will probably return false at end of file, since streams have built in bool operators to check if the stream is still good for I/O operations... the reason I say probably is because I usually use cin.getline, not the global string function getline that you're using.
-Greywolf

I usually use std::getline, and yes it's true it returns the istream object.

I commmonly do this:

string inputLine;
while( getline(inFile,inputLine) )
{
//... Process Line
}

Hello

Using :

cout << "enter 5 letter";
getline(cin, letter);

executes fine but when running PC- Lint:
getline(cin, expectedLabel): error 534: (Warning -- Ignoring return value of function 'std::getline(std::basic_istream<char,std::char_traits<char>> &, std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)'
error 830: (Info -- Location cited in prior message)

I was wondering if anyone could tell me the problem with getline and is there an alternitve i do wish to enter 5 letters

It's a false positive. Lint programs are designed to be persnickety, which often means warning about problems that simply don't exist. Many times you'll call a function that returns a value for the side effect rather than the return value. getline in this case, has a side effect of reading a string from the first argument into the second argument. For convenience purposes it also returns a reference to the stream (the first argument), but ignoring that return value won't cause any problems.

To silence the warning you can cast the function call to void:

cout << "enter 5 letter";
(void)getline(cin, letter);

However, that's assuming you're properly checking the stream state later on. If getline fails for some reason, you can use the stream state to determine what happened and avoid potential errors from assuming success. This is a case where the return value is useful:

cout << "enter 5 letter";
if (!getline(cin, letter)) 
{
    // Handle the error
}

Which is equivalent to, and less verbose than this:

cout << "enter 5 letter";
(void)getline(cin, letter);

if (!cin) {
    // Handle the error
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.