Linked list help

This is a discussion on Linked list help within the C Programming forums, part of the General Programming Boards category; I have to write a program for a monopoly game and my program is almost finished, I just have a ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    21

    Linked list help

    I have to write a program for a monopoly game and my program is almost finished, I just have a few bugs that I need to workout that I can't seem to debug.
    I was wondering if anyone here couple help me out a bit?
    Basically the program is a monopoly type game where you can buy properties (basically just any name you type in) sell any properties you own, and improve properties by adding hotels and houses.
    The program does not have to keep track of players money but all the information needs to be stored in linked lists.
    Any ideas on what I have done wrong?

    A few random things print out, especially on the buy option and view_properties isn't working

    Code:
    #include <stdio.h>
    #include  <stdlib.h>
    #include <string.h>
    
    #define HOUSE  25
    #define HOTEL  50
    #define PROPERTY  100
    
    struct property
    {
        char name[30];
        int user;
        int num_houses;
        int num_hotels;
        struct property * next;
    };
    
    struct property * buy(struct property *list, char property_name[30], int user);
    struct property * add_node(struct property *list, char property_name[30], int user);
    struct property * improve(struct property *list, char property_name[30], int user);
    struct property * sell(struct property *list, char property_name[30], int user);
    struct property * del(struct property *list, char property_name[30]);
    void print_properties(struct property *list);
    
    
    //Buy function (search for name and add to linked list)
    //Improve function (Add hotel or house, add to linked list)
    //Sell function (Print amount property is worth + hotels and houses, remove property from linked list)
    
    int main () {
        int num_players, selection, user;
        char property_name[30];
        struct property  *mylist = NULL;
    
        printf("Welcome to Monopoly!\n");
        printf("How many players will there be?\n");
        scanf("%d", &num_players);
    
        printf("Enjoy your game!\n");
        user = 1;
    
        do
        {
            if (selection == 5)
                user ++;
            if (user > num_players)
                user = 1;
    
            printf("Player %d what would you like to do?\n", user);
    
            printf("\t1-Buy Property\n");
            printf("\t2-Improve Property\n");
            printf("\t3-Sell Property\n");
            printf("\t4-View Properties\n");
            printf("\t5-End Turn\n");
            printf("\t6-End Game\n");
            scanf("%d", &selection);
    
            if (selection == 1) {
                printf("Please enter the name of the property. (No spaces)\n");
                scanf("%s", &property_name);
                mylist = buy(mylist, property_name, user);
                }
    
            if (selection == 2) {
                printf("Which property would you like to improve?\n");
                scanf("%s", &property_name);
                mylist = improve(mylist, property_name, user);
            }
    
            if (selection == 3)  {
                printf("Which property would you like to sell?\n");
                scanf("%s", &property_name);
                mylist = sell(mylist, property_name, user);
            }
    
            if (selection == 4)
                print_properties(mylist);
                printf("Player %d what would you like to do?\n", user);
                printf("\t1-Buy Property\n");
                printf("\t2-Improve Property\n");
                printf("\t3-Sell Property\n");
                printf("\t4-View Properties\n");
                printf("\t5-End Turn\n");
                printf("\t6-End Game\n");
                scanf("%d", &selection);
    
    
    
    //Selection Results
    
        }
        while (selection != 6);
        //if (selection == 6)
        //End game function
    
    
        return 0;
        }
    
    
        struct property * buy(struct property *list, char property_name[30], int user){
    
    
        struct property *helper = list;
    
        int value;
    
    if(list == NULL)
        add_node(list, property_name, user);
    
    while(helper != NULL){
            value = strcmp(property_name, helper->name);
            printf("%d\n", value);
             if (value == 0){
                printf("Player %d owns that property!", helper->user);
                return;
        }
        helper = helper->next;
        }
    
    
    
    return(list);}
    struct property * add_node(struct property * list, char property_name[30], int user){
    
    struct property * temp = (struct property *)malloc(sizeof(struct property));
        strcpy(temp->name, property_name);
        printf("%s\n", temp->name);
        temp->user = user;
        printf("%d\n", temp->user);
        temp->num_houses = 0;
        printf("%d\n", temp->num_houses);
        temp->num_hotels = 0;
        printf("%d\n", temp->num_hotels);
        temp->next = NULL;
    
        if(list == NULL){
            list = temp;
        }
        else{
            temp->next = list;
            list = temp;
        }
    return list;
    }
    struct property * improve(struct property *list, char property_name[30], int user){
    
        printf("improve property");
    
        struct property *helper = list;
        int value, improve_choice, num_houses_buy, num_hotels_buy;
    
        if(helper == NULL){
            return list;}
    
        while(helper != NULL){
            value = strcmp(property_name, helper->name);
            printf("value = %d", value);
            if (value == 0){
                if(helper->user == user){
                    printf("Do you want to buy:\n1:House\n2:Hotel\n");
                    scanf("5d", &improve_choice);
                        if(improve_choice == 1){
                            printf("How many houses would you like to buy?");
                            scanf("%d", &num_houses_buy);
                            helper->num_houses = helper->num_houses + num_houses_buy;
                        }
                        if(improve_choice == 2){
                            printf("How amny hotels would you like to buy?");
                            scanf("%d", &num_hotels_buy);
                            helper->num_hotels = helper->num_hotels + num_hotels_buy;
                        }
                }
            }
            else
                helper = helper->next;
        }
    
    return list;
    }
    struct property * sell(struct property *list, char property_name[30], int user){
    
        printf("sell_property");
    
        struct property *helper = list, *temp;
        int value, property_sold;
    
        while(helper != NULL){
            value = strcmp(property_name, helper->name);
            helper = helper->next;
            if (value == 0){
                if(helper->user == user)
                    property_sold = helper->num_houses*25 + helper->num_hotels*50 + 100;
                    del(list, property_name);
                    printf("You sold %s for %d dollars!", helper->name, property_sold);
                return;
        }
        }
    
    return;}
    struct property * del(struct property *list, char property_name[30]){
        struct property * helper = list, *deleted;
    
        if (helper != NULL) {
            if (strcmp(helper->name, property_name) == 0) {
                list = helper->next;
                free(helper);
                return list;
        }
             while (helper-> next != NULL) {
                if (strcmp(helper->next->name, property_name) == 0) {
                    deleted = helper->next;
                    helper->next = deleted->next;
                     free(deleted);
                return list;
    }
            helper = helper->next;
             }
        }
        return list;
    }
    void print_properties(struct property *list){
        printf("print properties");
        struct property *helper = list;
        while(helper != NULL){
            printf("%s is owned by player %d.", helper->name, helper->user);
            helper = helper->next;
        }
    return;
    }
    Thank you in advance for help

  2. #2
    Registered User poornaMoksha's Avatar
    Join Date
    Sep 2011
    Location
    India
    Posts
    41
    You have not initialized the selection variable

    Code:
    int main () {
        int num_players, selection, user;
    ...
    ...
    and you are using it in do-while loop

    Code:
    do
        {
            if (selection == 5)
    ...
    ...

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    For a start, here are the warnings I get when I compile your code:
    Code:
    $ gcc -ggdb3 -W -Wall -Wextra -o test test.c
    test.c: In function ‘main’:
    test.c:61:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Wformat]
    test.c:67:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Wformat]
    test.c:73:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Wformat]
    test.c: In function ‘buy’:
    test.c:117:13: warning: ‘return’ with no value, in function returning non-void [-Wreturn-type]
    test.c: In function ‘improve’:
    test.c:163:17: warning: too many arguments for format [-Wformat-extra-args]
    test.c: In function ‘sell’:
    test.c:197:13: warning: ‘return’ with no value, in function returning non-void [-Wreturn-type]
    test.c:201:1: warning: ‘return’ with no value, in function returning non-void [-Wreturn-type]
    test.c:186:38: warning: unused variable ‘temp’ [-Wunused-variable]
    Lines 61, 67, 73:
    Code:
    scanf("%s", &property_name);
    "property_name" is already a pointer.

    Line 163: typo

    Rest of the warnings are self-explanatory.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 11:13 AM
  2. Replies: 4
    Last Post: 05-01-2010, 11:19 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 09:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM

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