Thread: Having an issue with being able to use multiple cin.getlines.

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    Having an issue with being able to use multiple cin.getlines.

    Hello. I'm writing a ZORK clone, and I am trying to use cin.getline() to obtain the program's inputs from the console. The basic piece of code I'm using for it is as follows:

    Code:
        char *str;
        cin.getline(str, MAXCOMMANDLENGTH, '\n');
        string command;
        command.assign(str);
    It works fine if it's only done once. When I try to use the same basic code more than once, it sometimes crashes and sometimes doesn't. I've tried using it both once and more than once in the same function, using it in multiple functions, messing with the variable names, both using and not using recursion, etc. Whether I can get it to work just fine on multiple occasions seems to follow no pattern whatsoever. The only thing I have noticed is that if I don't change anything at all about the code, then its reliability is consistent.

    An example is as shown:

    Code:
    string Engine::getInput()
    {
        char *str1;
        cin.getline(str1, MAXCOMMANDLENGTH, '\n');
        string command;
        command.assign(str1);
        return command;
    }
    
    void Engine::requestResponse()
    {
        char *str;
        cin.getline(str, MAXCOMMANDLENGTH, '\n');
        string command;
        command.assign(str);
        interpretAll(command);
    }
    That works fine. Another example:

    Code:
    string Engine::getInput()
    {
        char *str1;
        cin.getline(str1, MAXCOMMANDLENGTH, '\n');
        string command;
        command.assign(str1);
        return command;
    }
    
    void Engine::requestResponse()
    {
        getInput(); // change
        getInput(); // change
        char *str;
        cin.getline(str, MAXCOMMANDLENGTH, '\n');
        string command;
        command.assign(str);
        interpretAll(command);
    }
    That also works fine. But this does not work:

    Code:
    string Engine::getInput()
    {
        char *str1;
        cin.getline(str1, MAXCOMMANDLENGTH, '\n'); // crashes here
        string command;
        command.assign(str1);
        return command;
    }
    
    void Engine::requestResponse()
    {
        char *str;
        cin.getline(str, MAXCOMMANDLENGTH, '\n');
        getInput(); // change
        string command;
        command.assign(str);
        interpretAll(command);
    }
    I have reason to suspect it has something to do with a segmentation fault. I have tried many different combinations of things (some of them being in the body of that interpretAll() function), but I have found no pattern. Does anybody know what's going on? Thanks!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's the problem:

    Code:
        char *str1;
        cin.getline(str1, MAXCOMMANDLENGTH, '\n');
    "str1" is just a char pointer; it has no memory allocated, so when you use this, you either overwrite some of your own memory, or you get a seg fault for trying to overwrite memory that is not yours. You want something more like:

    Code:
        char str1[MAXCOMMANDLENGTH];
        cin.getline(str1, MAXCOMMANDLENGTH, '\n');
    However, that is still kind of pointless considering you can just do this:

    Code:
    	string command;
    	getline(cin, command);
    This is different than the istream.getline in that it uses a C++ string, and different from the >> operator in that it reads to a newline (ie, includes spaces).
    Last edited by MK27; 10-26-2011 at 12:22 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    100
    Thanks, man! That did the trick. I'm a little confused though on why C++ would allocate the memory correctly one time, and then be unreliable about it afterwards.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jsrig88 View Post
    Thanks, man! That did the trick. I'm a little confused though on why C++ would allocate the memory correctly one time, and then be unreliable about it afterwards.
    It does not allocate any memory at all, in any case. The difference is between what got overwritten on your stack; this will affect different things. Eg, if the overwritten space was unused, you would not notice.

    BTW, check my last post again, I edited it to include a note about the global string library getline(), which is more useful here than the istream getline().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  2. Getlines and looping
    By Blanked in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2009, 07:26 PM
  3. Multiple inheritance issue
    By Helix in forum C++ Programming
    Replies: 14
    Last Post: 03-31-2009, 02:21 PM
  4. Replies: 1
    Last Post: 05-01-2003, 02:52 PM
  5. getlines..
    By BODYBUILDNERD in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2003, 12:59 PM

Tags for this Thread