Trying to get a random element from the 2d array called fruits, im not sure if the printf statemetn with the %c is correct since it just prints a character? im not sure what else to use. Any help appreciated Edit: Trying to get a random word instead of a random letter from the fruits array, sorry if unclear.
#include <stdio.h>
int main()
{
srand(time(NULL));
int isSpin;
char random;
printf("To spin the wheel input 1\n");
scanf("%d", &isSpin);
char fruits[4][10] = {"bell", "orange", "cherry", "horseshoe"};
if (isSpin == 1)
{
random = fruits[rand()%4];
printf("%c", random);
}
else
{
printf("You didn't spin");
}
}
CodePudding user response:
char random;
/* ... */
random = fruits[rand()%4];
printf("%c", random);
random
is assigned with pointer fruits[rand()%4]
converted to integer. It definitely is not something you try to archive.
you want
int word = rand()%4;
random = fruits[word][rand() % (strlen(fruits[word]) - 1)];
Yeah, im just trying to print one of the 4 strings at random not a singular character, sorry if unclear
Why is it not in the question?
char *random;
/* ... */
random = fruits[rand()%4];
printf("%s\n", random);
CodePudding user response:
To print a string you have to use %s
printf("%s", random);