aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/buy.c41
-rw-r--r--src/sell.c5
-rw-r--r--src/util.c2
3 files changed, 26 insertions, 22 deletions
diff --git a/src/buy.c b/src/buy.c
index a63161f..267df73 100644
--- a/src/buy.c
+++ b/src/buy.c
@@ -14,6 +14,7 @@ void buy_handler(struct player_t *player)
if(!get_stock_info(sym, &price, &name))
{
printf("Failed to get query information for '%s'.\n", sym);
+ free(sym);
return;
}
@@ -45,36 +46,32 @@ void buy_handler(struct player_t *player)
{
printf("Confirmed.\n");
- struct stock_t *stock;
+ struct stock_t *stock = find_stock(player, sym);
/* add the stock to the portfolio or increase the count of a stock */
- for(uint i = 0; i < player->portfolio_len; ++i)
+ if(stock)
{
- if(strcmp(player->portfolio[i].symbol, sym) == 0)
- {
- stock = player->portfolio + i;
- stock->count += count;
- stock->current_price.cents = price.cents;
- goto done;
- }
+ stock->count += count;
+ stock->current_price.cents = price.cents;
}
+ else
+ {
+ /* stock is not in portfolio yet, add it */
- /* not found, add a new one */
- player->portfolio_len += 1;
- player->portfolio = realloc(player->portfolio, player->portfolio_len * sizeof(struct stock_t));
- player->need_to_free_portfolio = true;
-
- stock = player->portfolio + player->portfolio_len - 1;
+ player->portfolio_len += 1;
+ player->portfolio = realloc(player->portfolio, player->portfolio_len * sizeof(struct stock_t));
+ player->need_to_free_portfolio = true;
- memset(stock, 0, sizeof(struct stock_t));
+ stock = player->portfolio + player->portfolio_len - 1;
- stock->symbol = sym;
- stock->fullname = name;
- stock->count = count;
- stock->current_price.cents = price.cents;
+ memset(stock, 0, sizeof(struct stock_t));
- done:
+ stock->symbol = sym;
+ stock->fullname = name;
+ stock->count = count;
+ stock->current_price.cents = price.cents;
+ }
player->cash.cents -= cost;
@@ -87,4 +84,6 @@ void buy_handler(struct player_t *player)
{
printf("Not confirmed.\n");
}
+
+ free(response);
}
diff --git a/src/sell.c b/src/sell.c
index ae5fe0e..d390419 100644
--- a/src/sell.c
+++ b/src/sell.c
@@ -14,14 +14,17 @@ void sell_handler(struct player_t *player)
if(!stock)
{
printf("Couldn't find '%s' in portfolio.\n", sym);
+ free(sym);
return;
}
+ free(sym);
+
printf("Updating prices...\n");
get_stock_info(stock->symbol, &stock->current_price, &stock->fullname);
- printf("You currently own %llu shares of '%s' (%s) valued at $%llu.%02llu each.\n",
+ printf("You currently own %llu share(s) of '%s' (%s) valued at $%llu.%02llu each.\n",
stock->count, stock->fullname, stock->symbol, stock->current_price.cents / 100, stock->current_price.cents % 100);
printf("How many shares do you wish to sell? ");
diff --git a/src/util.c b/src/util.c
index 8f82976..0740a09 100644
--- a/src/util.c
+++ b/src/util.c
@@ -263,5 +263,7 @@ ullong read_int(void)
ullong ret = -1;
sscanf(str, "%llu", &ret);
+ free(str);
+
return ret;
}