Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    allocate memory using getline

    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.
    Last edited by khalifa; 06-01-2007 at 08:50 PM. Reason: strncmp is replaced with strncpy

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    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:

    Code:
    #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.
    OracleGuy

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    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.
    Last edited by khalifa; 06-02-2007 at 07:43 AM. Reason: while condition was not right

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Don't use character arrays in C++, use the string class. (Don't use arrays of anything)

    Code:
    #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:

    Code:
    	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.
    Last edited by ralph l mayo; 06-02-2007 at 10:21 AM.

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by ralph l mayo View Post
    Don't use character arrays in C++, use the string class. (Don't use arrays of anything)
    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.
    Last edited by oracleguy; 06-03-2007 at 07:17 AM.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •