-2

I'm having difficulties using strcmp. I make a call in a separate else statement (not listed), and it works fine. Is this a possible memory issue?

while(inHere == 1)
{
    int numberOfOccupiedTables = 0;

    cout << "\nSelect a table below\n---------------\n\n";

    for(int i = 0; i < tables->size(); i++)
    {
        if(tables->at(i)->open == 0)
        {
            cout << "Table " << tables->at(i)->value << "\n";
            numberOfOccupiedTables++;
        }
    }

    if(numberOfOccupiedTables == 0) 
        cout << "No customers found.\n";
    else
    {
        cout << "(q to back out) Enter number of table: ";
        char* choice = (char*)malloc(sizeof(char)*256);
        fgets(choice,256,stdin);
        if(strcmp(choice, "q\n") == 0)
            inHere = 0;
    }
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • What's the error message? – OMGtechy Apr 26 '14 at 19:47
  • Seems like GIGO: `strcmp()` expects 0-terminated strings. – Deduplicator Apr 26 '14 at 19:47
  • When you step to the call to `strcmp`, the pointer and respective memory looks like what? – CPlusPlus OOA and D Apr 26 '14 at 19:47
  • I don't suppose you verified the `fgets()` actually *worked* ? – WhozCraig Apr 26 '14 at 19:47
  • As an aside, `sizeof(char)` is per definitionem 1, no need to ask. – Deduplicator Apr 26 '14 at 19:49
  • Something else to consider is explaining the mixture of C library calls within C++. The `std::string` could make problems go away. – CPlusPlus OOA and D Apr 26 '14 at 19:49
  • The program successfully completes the comparison since I can print strings after the call to strcmp, so that verifies that fgets works correctly as well. – user3576841 Apr 26 '14 at 19:51
  • @CPlusPlusOOAandD Those are respected C++ library calls too. Still, changing to a higher layer of abstraction is probably good. – Deduplicator Apr 26 '14 at 19:51
  • @user3576841 Why did you allocate memory for `choice`? All you need to do is declare a char array without calling malloc(). `char choice[256];` – PaulMcKenzie Apr 26 '14 at 19:51
  • Nope, flawed reasoning @user3576841. If it fails, the buffer pointed to by `choice` is indeterminate (no idea why you don't use an automatic array there). – Deduplicator Apr 26 '14 at 19:52
  • So why do you think the crash is due to `strcmp()` if the program continues? As @OMGtechy asked, what's the error message? – Paul Roub Apr 26 '14 at 19:54
  • The very premise of the question is the `strcmp()` doesn't *return* and the program *crashes* while invoking it. But what did `fgets()` **return** ? (i.e. the consequences of failing to abide by [Henry Spencer's Sixth Commandment](http://www.lysator.liu.se/c/ten-commandments.html)). If it returned an error condition, the output buffer content is *undefined*, and since you never terminate it.... – WhozCraig Apr 26 '14 at 19:55
  • BTW: Shall `inHere` be a boolean variable (true/false)? In that case, follow the language, only 0 is false, and there is no need to check for !=0 in a conditional expression. – Deduplicator Apr 26 '14 at 19:55
  • @WhozCraig note the comment where OP says "I can print strings after the call to strcmp" – Paul Roub Apr 26 '14 at 19:57
  • @Deduplicator I like to be language consistent, especially with C++11. Hopefully user3576841 has a good design reason. Alternatively, user3576841 will expand thinking about the C++ library more. – CPlusPlus OOA and D Apr 26 '14 at 20:01
  • @PaulRoub I noted that while also noting the title of the question, which seems to introduce a bit of a paradox compared with that comment. Its one or the other. I'm almost curious to know which it *really* is. – WhozCraig Apr 26 '14 at 20:03
  • @CPlusPlusOOAandD It is language consistent, just (possibly) unneccessarily low-level. Still, we cannot know under what burdens the OP labors, nor should we excise parts of C++. Learning of *all* levels is neccessary to ever achieve proficiency. – Deduplicator Apr 26 '14 at 20:04
  • The Error that I get after running this (writing in NP++, running the bloodshed dev c++) is that the runtime has ended the program in the unusual manner – user3576841 Apr 26 '14 at 20:07
  • @user3576841 and you believe that's due to `strcmp()` because...? – Paul Roub Apr 26 '14 at 20:08
  • Why, where, and with what message? I recommend reading [How to Debug Small Programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs) to get a better idea of how to debug this. – Deduplicator Apr 26 '14 at 20:08

1 Answers1

1

If fgets() fails because it reached the end of the file, it will not add a null terminator to the string. Check to make sure it hasn't returned NULL before you do the strcmp().

Zebra North
  • 11,412
  • 7
  • 37
  • 49
  • 1
    Standard quote: The fgets function returns s if successful. If end-of-file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned. If a read error occurs during the operation, the array contents are indeterminate and a null pointer is returned. – Deduplicator Apr 26 '14 at 20:12
  • @Deduplicator I concur. The content does indeed remain unchanged if EOF is reached and nothing has been transferred thus-far. Unfortunately for the OP, that content is entirely indeterminate to begin with if that is indeed happening, per the behavior of `malloc()`. – WhozCraig Apr 26 '14 at 22:02