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;
}
}