My professor has tasked us with creating a story using the user's inputted personal information and the srand() and rand() functions.
This is the code I have written. I expected the story to be written out and the %s's to be filled in, instead some of them are multiple strings types out together with no spaces, and some are "(NULL)".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_LEN 64
#define ARRAY_SIZE 7
/*This story will go something like this:
Flying through the sky, a <bird> looks down upon <current city name>
and spots <name>. <name> is <age> years old and is studying to
become a <career title>. <name> is on their way to <hometown name>
to visit their <number> family members. As <name> packs their stuff
into a <color> <car>, their pet <favorite animal>, <pet name> gets in the vehicle,
and starts to <animal sounds>. <name> pets <pet name> and gets in the vehicle.
<name> starts the car and turns the radio up. <name> and <pet name> listens to <song>
for the whole <number> hour long trip. Once <name> and <pet name> arrived, <ending>.*/
int main(){
char* questions[ARRAY_SIZE]={
"what is your name?", //0
"what is your favorite animal?", //1
"where were you born?", //2
"how old are you?", //3
"where do you currently live?", //4
"what do you want your future career to be?", //5
"what is your pet's name?", //6
};
const int NAME = 0, ANIMAL = 1, BIRTHPLACE = 2, AGE = 3, HOME = 4, CAREER = 5, PET_NAME = 6;
char* bird[ARRAY_SIZE]={
"hawk" //0
"eagle" //1
"pigeon" //2
"sparrow" //3
"cardinal" //4
"raven" //5
"pelican" //6
};
char* color[ARRAY_SIZE]={
"red" //0
"orange" //1
"yellow" //2
"green" //3
"blue" //4
"indigo" //5
"violet" //6
};
char* car[ARRAY_SIZE]={
"Hyundai Tucson" //0
"Ford Mustang" //1
"Dodge Charger" //2
"Ford Focus" //3
"Chevrolet Tahoe" //4
"Subaru Outback" //5
"Buick Park Avenue" //6
};
char* sounds[ARRAY_SIZE]={
"barking" //0
"meowing" //1
"cooing" //2
"roaring" //3
"growling" //4
"cawing" //5
"squeaking" //6
};
char* song[ARRAY_SIZE]={
"\"don\'t stop believin\'\" by journey" //0
"\"bohemian rhapsody\" by queen" //1
"\"under the bridge\" by red hot chili peppers" //2
"\"logan circle\" by the wonder years" //3
"\"shout at the devil\" by motley crue" //4
"\"do re mi\" by blackbear" //5
"\"the time (dirty bit)\" by the black eyed peas" //6
};
char* number[ARRAY_SIZE]={
"one" //0
"two" //1
"three" //2
"four" //3
"five" //4
"six" //5
"seven" //6
};
char* endings[ARRAY_SIZE]={
"they all had a bonfire" //0
"they all watched a movie" //1
"they all went out to eat" //2
"they all ate dinner at home" //3
"they all watched the stars at night" //4
"they all set off fireworks" //5
"they all went to bed to continue visiting in the morning" //6
};
char answers[ARRAY_SIZE][MAX_LEN];
for(int i=0; i<ARRAY_SIZE; i++){
printf("%s\n", questions[i]);
fgets(answers[i],MAX_LEN,stdin);
gets(answers[i]);
answers[i][strlen(answers[i])-1]='\0';
}
srand(time(NULL));
printf("=================================================================\n");
printf("Flying through the sky, a %s looks down upon %s\n",bird[rand()%ARRAY_SIZE], answers[HOME]);
printf("and spots %s. %s is %s years old and is studying to\n",answers[NAME], answers[NAME], answers[AGE]);
printf("become a %s. %s is on thier way to %s\n",answers[CAREER], answers[NAME], answers[BIRTHPLACE]);
printf("to visit their %s family members. As %s packs their stuff\n",number[rand()%ARRAY_SIZE], answers[NAME]);
printf("into a %s %s, their pet %s, %s gets in the vehicle,\n",color[rand()%ARRAY_SIZE], car[rand()%ARRAY_SIZE], answers[ANIMAL], answers[PET_NAME]);
printf("and starts to %s. %s pets %s and gets in the vehicle.\n",sounds[rand()%ARRAY_SIZE], answers[NAME], answers[PET_NAME]);
printf("%s starts the car and turns the radio up. %s and %s listens to %s\n",answers[NAME], answers[NAME], answers[PET_NAME], song[rand()%ARRAY_SIZE]);
printf("for the whole %s hour long trip. Once %s and %s arrived, %s.",number[rand()%ARRAY_SIZE], answers[NAME], answers[PET_NAME], endings[rand()%ARRAY_SIZE]);
return 0;
In analyzing your code, the short answer is that your various literal arrays needed to have each string set delimited with a comma.
For example, in your code the "birds" array is as such:
but should be like the following:
When I tried out your code without the delimiters, I was getting a bunch of "null" detail. Also, as noted in the comments, there doesn't seem to be a reason for the "gets" call. With that in mind, following is the refactored code with the various comma delimited string values.
Placing the whole refactored code into this answer makes it lengthy, but I felt it was important to view. With the code revised, following was a terminal input and output for a test run.
To recap, be cognizant of how character arrays need to be initialized. Perhaps you might want to delve into some tutorial literature to get a better understanding of arrays, especially character arrays.