Edit: Thank you so so much (everyone!) for your comments! I was able to fix the win percentage and the wonky array numbers with your guidance. however, I have yet to fix the average # of bets it takes to win or lose. I updated my code, now below.
Begin original post:
Thank you in advance to anyone who reads this or reaches out with advice!
So I am doing the stereotypical gamble until you win or it is all gone dealie for an intro to programming course. The code works great, except when I actually add the heads or tails part - then my array that holds numbers of bets gets messed up. When I use notes to hide the actual coin toss part, the array works fine. When I run it with the coin toss, the betArray stores really wonky numbers (negative integers, integers in the billions).
Here is the code so far:
#include<stdio.h>
#include<stdlib.h> /*Enables use of rand()*/
int main () {
setvbuf(stdout, NULL, _IONBF, 0); /*Allow me to use repl.it*/
/*Enter Gambler's name*/
char gambleName[15] = "";
printf("Enter gambler's name:\n");
scanf("%s", gambleName);
printf("\nWelcome, ");
printf("%s! \n", gambleName);
/*Enter Stakes*/
int availableFunds;
int goalFunds;
printf("We'll be betting $1 per bet. Enter your available funds:\n");
scanf("%d", &availableFunds); /* Saves stakes as availableFunds */
int seedVal=4;
srand(seedVal);
/*Butter the gamblers up*/
if(availableFunds>=1) {
printf("%d? Wow, %s - that's a great start!\n",availableFunds, gambleName);
/*Enter Goal*/
printf("How much do you want to win today? Enter a value up to 10000 with no commas, decimals or spaces:\n"); /*Saves goal as goalFunds*/
scanf("%d",&goalFunds);
/*Recognize ambitious gamblers*/
if (goalFunds > 10*availableFunds) {
printf("Wow, ambitious! Let's get started.\n");
}
else {
printf("OK, let's get started!\n");
}
printf("\n");
/*begin gambling problem*/
int betArray[1000]={0};
int game = 0;
int bet=0;
float wins = 0;
int losses = 0;
for (game=0 ; game<1000; game++) {
if (availableFunds>0 && availableFunds<goalFunds) {
int toss = rand()%2;
bet+=1;
/*losing bet*/
if (toss == 0) {
availableFunds -= 1;
losses += 1;
}
/*winning bet*/
else {
availableFunds += 1;
wins += 1;
}
betArray[game+1] = bet;
}
else {
break;
}
}
int sumBet = 0;
for (game=0;game<1000;game++) {
sumBet+=betArray[game];
}
float betAverage = sumBet/1000;
float winOutOfGames = wins/1000;
float winPercent = winOutOfGames*100;
/*print totals*/
printf("%d games played with:\n",game);
printf("%.f goals reached\n",wins);
printf("%d down-and-out losses.\n",losses);
printf("You won ~%.1f%% of your games.\n",winPercent);
printf("On average, it took you %.f bets to win or go broke.\n",betAverage);
printf("\n");
printf("Thanks for playing!\n");
for (game = 1; game <= 50; game++) {
printf("Bets in game [%d] = %d\n",game,betArray[game]);
}
}
/* Send the broke guys packing*/
else {
printf("$%d...? You may need to stop at an ATM... ¯\\_(ツ)_/¯ See you next time!\n", availableFunds);
}
return 0;
}
Sorry if the code is a little messy, I added some stuff to try and wanted to send the latest version.
Thanks!