...

View Full Version : allocate memory using getline



khalifa
06-01-2007, 09:49 PM
I have problem with getline command. it is like this:

getline(input,5,'\n');

the problem is the user input. The input requires only 4 characters. what if
the user enters more than that? I tried to enter more than 4 characters. what
happened is that the program crashes. I solved this problem like this.

getline(big_value,1000,'\n');
strncpy(input,big_value,4);

it can be seen it is not exactly the right solution but if there is way to
allocate memory for the input using getline command, I will appreciate the replies
or please put a link to read about this problem.

oracleguy
06-02-2007, 02:20 AM
If the user enters more than 5, it shouldn't crash the program. In fact I tested it to make sure, it doesn't.

The whole point to providing the size of the buffer is so that it doesn't overflow the buffer.

Here is the code I tested it with:



#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
char buffer[10];

cout << "Enter in a value: ";

cin.getline(buffer, 10, '\n');

cout << "Buffer contents: " << buffer << endl;

system("pause");
return 0;
}

If you try this, you'll see the buffer never takes in more than 9 characters (it needs the last position for the null).

You just have to make sure to clear the input buffer on cin after a getline because the extra characters the user entered are still in the buffer and will get read first if you do another cin.

khalifa
06-02-2007, 08:39 AM
thanks for reply. I copied your code and it worked perfectly. however, when
I tested in my program it crashes. this happened when using getline in a loop.
You can check this code. I used developer c++ (dev c++) to compile the
code.



#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
char buffer[10];

while(1){

cout << "Enter in a value: ";

cin.getline(buffer, 10, '\n');

cout << "Buffer contents: " << buffer << endl;

}

system("pause");
return 0;
}



thank you for your reply.

ralph l mayo
06-02-2007, 10:25 AM
Don't use character arrays in C++, use the string class. (Don't use arrays of anything (http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1))



#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::cin;
using std::endl;

int main()
{
string buffer;

while(1)
{
cout << "Enter in a value: ";

getline(cin, buffer);
buffer = buffer.substr(0, 10); // If you only want 10 characters

cout << "Buffer contents: " << buffer << endl;
}

return 0;
}


For the record, you code was not failing because of a buffer overflow, cin.getline() protected you from that, it failed because of how cin.getline() actually implements the character limit.

It doesn't truncate the part of the stream you asked it to read, it just fails, and sets the fail bit on the istream (cin). When it loops back around, cin.fail() is still true so it doesn't bother getting a line, and it doesn't even block, it just does the cout statements again and again as fast as it can, since that stream is still working. You can make old school getline work for you like this, but don't:



while(1)
{
cout << "Enter in a value: ";

if (!cin.getline(buffer, 10, '\n'))
{
cout << "Less than 10 characters plz, somebody wrote half this program in C :[" << endl;
cin.clear();
cin.ignore(LONG_MAX, '\n'); // Discard the excess characters so as not to create more entries from the same botched input. Fails for very long input
}
else
{
cout << "Buffer contents: " << buffer << endl;
}
}


edit: just to re-emphasize I'm not endorsing that C code. I'm not a C programmer and I imagine there are better ways to handle that failure, I just did it to demonstrate the source of the original problem.

oracleguy
06-03-2007, 08:13 AM
Don't use character arrays in C++, use the string class. (Don't use arrays of anything (http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1))

Interesting article; however I think never, ever using any sort of arrays and always using containers is kind of silly. While I agree a container is a easier/better idea if you are using dynamic arrays, it isn't if you are using a fixed size array. It only takes a half decent programmer to write code that won't explode while using arrays.

I actually don't care for the STL string class myself since it lacks a lot of bonehead things that would make it 100x easier to work with. So I typically stick to using a different string class when I can.

But I do agree on the containers thing, I have a templated array class I wrote a long while ago that I use all the time, makes it easy to work with.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum