Thread: Function logic error

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Function logic error

    The relevant part of my code:

    Code:
    #include <stdio.h>
    
    int getSize(void);
    void errorReport(int size);
    
    int main(int argc, char *argv[]) {
        int size = 0;
        
        size = getSize();
        printf("In main, size = %d\n", size);
    
        return 0;
    }
    
    int getSize(void) {
        printf("Enter the grid size, an odd number between 3 and 15: ");
        int size = 0;
        scanf("%d", &size);
        printf("In getSize 1, size = %d\n", size);
        errorReport(size);
        printf("In getSize 2, size = %d\n", size);
        return size;
    }
    
    void errorReport(int size) {
        if ( (size < 3) || (size > 15) || ((size % 2) == 0) ) {
            getSize();
        }
    }
    What it should be doing is first prompting the user,

    Enter the grid size, an odd number between 3 and 15:
    And if the user enters an invalid number, it'll just ask again. Once the user enters a valid number it should then assign it to the variable size and pass it back into the main function.

    However, here is some input and the output when I run the code:

    Enter the grid size, an odd number between 3 and 15: 2
    In getSize 1, size = 2
    Enter the grid size, an odd number between 3 and 15: 4
    In getSize 1, size = 4
    Enter the grid size, an odd number between 3 and 15: 100
    In getSize 1, size = 100
    Enter the grid size, an odd number between 3 and 15: 5
    In getSize 1, size = 5
    In getSize 2, size = 5
    In getSize 2, size = 100
    In getSize 2, size = 4
    In getSize 2, size = 2
    In main, size = 2
    Notice that it's keeping the incorrect values I entered previously and so the value of size being passed into the main function is the first value I entered.

    I just don't understand why it's doing this...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    errorReport calls getSize, but ignores the return result.

    You're better off putting an actual loop in getSize(), and make errorReport() return a simple boolean indicating whether size is OK or not.
    If it's OK, you return from getSize(), otherwise you loop for another value.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    I changed my code to

    Code:
    int getSize(void) {
        printf("Enter the grid size, an odd number between 3 and 15: ");
        int size = 0;
        scanf("%d", &size);
        printf("In getSize 1, size = %d\n", size);
        int fail = 0;
        fail = errorReport(size);
        if (fail == 1) {
            getSize();
        }
        printf("In getSize 2, size = %d\n", size);
        return size;
    }
    
    //Check if the size entered is valid
    int errorReport(int size) {
        int fail = 0;
        if ( (size < 3) || (size > 15) || ((size % 2) == 0) ) {
            fail = 1;
        }
        return fail;
    }
    But I'm still getting the same result. Also, I don't quite understand what you mean by the errorReport function calling getSize but ignoring the return result? If only I had an intuitive understanding of what these functions are doing, then maybe I'd be able to see what was going wrong.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A recursive call is not the kind of loop you want.

    Code:
    do {
      // some stuff here
    } while ( fail != 0 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Sorry, but I can't see how to apply your suggestion, could you please clear things up a little bit?
    I don't understand why the previous values of size are being kept, I thought they would be overridden...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Either of these would work.
    Code:
    int getSize(void) {
        printf("Enter the grid size, an odd number between 3 and 15: ");
        int size = 0;
        scanf("%d", &size);
        printf("In getSize 1, size = %d\n", size);
        int fail = 0;
        fail = errorReport(size);
        if (fail == 1) {
            size = getSize();
        }
        printf("In getSize 2, size = %d\n", size);
        return size;
    }
    
    int getSize(void) {
        printf("Enter the grid size, an odd number between 3 and 15: ");
        int size = 0;
        int fail = 0;
        do {
            scanf("%d", &size);
            printf("In getSize 1, size = %d\n", size);
            fail = errorReport(size);
        } while ( fail != 0 );
        printf("In getSize 2, size = %d\n", size);
        return size;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    In the first one, the only difference was that you replaced getSize() with size = getSize(). I'll try to keep this procedure in mind so I don't trip over it again in the future.

    I didn't know do was a loop function, in your previous post I thought you were literally telling me to do something lol.

    Thanks Salem, you've been a great help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logic Error Help
    By Taka in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2009, 10:13 PM
  2. logic Error
    By asmaa in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2009, 10:58 PM
  3. Logic Error...
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 01-28-2006, 06:21 AM
  4. logic error I think
    By sscook69 in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2002, 10:08 PM
  5. logic error
    By sscook69 in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2002, 06:00 PM