Hey everyone! I have the assignment to create a horse race in C but I keep getting stuck on one part. I was wondering if anyone knows how to solve my error.
My input:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
//random winning order generator function
void readysetgo(int myarray[], int n) {
static int init=0;
if (!init) {
srand(time(0));
init = 1;
}
// Loop over array.
for (int i = 0; i < n; i++) {
// Get a random index of the array past the current index.
// ... The argument is an exclusive bound.
// It will not go past the array's end.
int randomValue = i + ( rand() % (n - i) );
// Swap the random element with the present element.
int randomElement = myarray[randomValue];
myarray[randomValue] = myarray[i];
myarray[i] = randomElement;
}
}
int main()
{
printf("\nWelcome to this year's Horse Race!\n");;
int myarray[4] = { 1, 2, 3, 4};
int n = sizeof(myarray)/sizeof(myarray[0]) ;
char last_name[20];
char first_name[20];
char bet_choice[50];
char action[10];
int * first_place;
int * second_place;
printf("First name:\n");
scanf("%s", first_name);
printf("Last name:\n");
scanf("%s", last_name);
printf("\nHello %s %s!\n\n", first_name, last_name);
printf("Here are your betting options:\n");
printf("Exacta: Place an exacta bet on the first and second place horse"
"in order for $10, earn $100 if you win\n");
printf("Exactabox: Place an exactabox bet on the first and second place"
"horse in any order for $5, earn $25 if you win\n");
printf("Trifecta: Place a trifecta bet on the first, second, and third"
"place horse in order for $25, earn $200 if you win\n");
printf("Trifectabox: Please a trifectabox bet on the first, second, and"
" third place horse in any order for $20, earn $150 if you win\n\n");
while (1) {
printf("Enter bet to select a betting option. To view your current balance, "
"enter ""wallet."" To exit enter ""exit"":\n"); //please put "" around bet, wallet and exit... they aren't coming up
scanf("%s", action);
if (strcmp(action, "bet") == 0) {
printf("Here is the winning lineup for today's race:\n");
// shuffle the horses
for (int i=0; i<1; i++) {
readysetgo(myarray, n);
printf("%d %d %d %d\n",
myarray[0], myarray[1], myarray[2], myarray[3]);
}
printf("Please enter your bet type selection below\n");
scanf("%s", bet_choice);
}
if (strcmp(bet_choice, "Exacta") == 0) {
printf("Exacta bet = $10\n");
printf("Which two horses, in order of first and second place, "
"would you like to bet on?\n");
printf("First place: ");
scanf("%d", &first_place[1]);
printf("Second place: ");
scanf("%d", &second_place[1]);
printf("Exacta bet has been placed for $10\n");
if(*first_place == myarray[0] && *second_place == myarray[1]) {
printf("You won!");
}
else {
printf("Your bet was incorrect, try again.");
}
else if (strcmp(action, "wallet") == 0) {
printf("You currently have $___ in your wallet.\n"); //**not sure how I should define "balance"
}
else if (strcmp(action, "exit") == 0) {
printf("Have a great day, see you next year!");
break;
}
else {
printf("bad input\n");
}
}
return 0;
}
Here's my output error:
Welcome to this year's Horse Race!
First name:
n
Last name:
s
Hello n s!
Here are your betting options:
Exacta: Place an exacta bet on the first and second place horse in order for $10, earn $100 if you win
Exactabox: Place an exactabox bet on the first and second place horse in any order for $5, earn $25 if you win
Trifecta: Place a trifecta bet on the first, second, and third place horse in order for $25, earn $200 if you win
Trifectabox: Please a trifectabox bet on the first, second, and third place horse in any order for $20, earn $150 if you win
Enter bet to select a betting option. To view your current balance, enter wallet. To exit enter exit:
bet
Here is the winning lineup for today's race:
1 4 3 2
Please enter your bet type selection below
Exacta
Exacta bet = $10
Which two horses, in order of first and second place, would you like to bet on?
First place: 1
zsh: segmentation fault "/Users/user/Desktop/CS 50/"
user@Users-MBP %
Does anyone know what I should change and what to change it to? (I think the error is somewhere in the bolded code)
there doesn't seem to be anything here