Can't tell why function is returning 0

This is a discussion on Can't tell why function is returning 0 within the C Programming forums, part of the General Programming Boards category; Alrighty, haven't posted here in forever! I'm working on a program for my CS1 class, it's a text based "monopoly ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    81

    Can't tell why function is returning 0

    Alrighty, haven't posted here in forever! I'm working on a program for my CS1 class, it's a text based "monopoly property tracker". I'm writing a function that improves the user inputted property, but for some reason it's saying that the current player doesn't own that property EVERY time. So for example, if I run the program and purchase a property, it adds a new node to the list storing the properties name, playerNum, etc. When I call another function printProperties, it prints the names of the properties and the player who owns it perfectly fine. I'm wondering why the printProperties function recognizes the player's property but my checkOwnership function (see bwlow) isn't. Sorry if there isn't enough info here, I'm sick and my head is kinda cloudy.

    Here's some relevant code:

    Code:
    struct property //THE STRUCT THE PROGRAM USES
        {
            char name[30];
            int user;
            int num_houses;
            int num_hotels;
            struct property * next;
        };
    
    else if(menuChoice == 2) //FROM MAIN
            {
                char propertyName[30];
    
                printf("Which property would you like to improve?\n");
                scanf("%s", propertyName);
    
                if(checkOwnership(myList, propertyName, currentPlayer) == 1)
                {
                    printf("Do you wish to build houses(1) or hotels(2)?\n");
                    int userChoice = scanf("%d", &userChoice);
    
                    if(userChoice == 1)
                    {
                        printf("How many houses do you wish you build?\n");
                        scanf("%d", &userChoice);
                        improveProperty(myList, propertyName, currentPlayer, userChoice, 0);
                    }
    
                    else if(userChoice == 2)
                    {
    
                        printf("How many hotels do you wish you build?\n");
                        scanf("%d", &userChoice);
                        improveProperty(myList, propertyName, currentPlayer, 0, userChoice);
                    }
                }
    
                else if(checkOwnership(myList, propertyName, currentPlayer) == 0)
                {
                    printf("You do not own that propety!\n");
                }
    }
    
    int checkOwnership(struct property * list, char name[], int player) //SEPARATE FUNCTION
    {
        struct property * helper = list;
    
        while(helper != NULL && strcmp(helper->name, name) != 0)
        {
            if(strcmp(helper->name, name) == 0)
            {
                if(helper->user == player)
                {
                    return 1;
                }
            }
    
            helper = helper->next;
        }
    
        return 0;
    }
    
    void improveProperty(struct property * list, char name[], int player, int numHouses, int numHotels) //SEPARATE FUNCTION
    {
        struct property * helper = list;
    
        while(helper != NULL && strcmp(helper->name, name) != 0)
        {
            if(strcmp(helper->name, name) == 0)
            {
                helper->num_hotels += numHotels;
                helper->num_houses =+ numHouses;
            }
    
            helper = helper->next;
        }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > while(helper != NULL && strcmp(helper->name, name) != 0)
    > if(strcmp(helper->name, name) == 0)
    How are you expecting two different answers out of the same function call?

    Remove the string comparison from the while loop condition.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    Oops. That did it, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function returning function pointer : help
    By ypp174 in forum C Programming
    Replies: 5
    Last Post: 09-17-2012, 01:10 AM
  2. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  3. Returning a function pointer from a function
    By StainedBlue in forum C++ Programming
    Replies: 4
    Last Post: 11-01-2010, 10:26 PM
  4. Error: _ defined as a function returning a function?
    By Jardon in forum C Programming
    Replies: 15
    Last Post: 07-29-2009, 11:53 AM
  5. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21