Why are my strings not printing in my story in c

61 Views Asked by At

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;

1

There are 1 best solutions below

0
NoDakker On

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:

char* bird[ARRAY_SIZE]={
    "hawk" //0
    "eagle" //1
    "pigeon" //2
    "sparrow" //3
    "cardinal" //4
    "raven" //5
    "pelican" //6
};

but should be like the following:

char* bird[ARRAY_SIZE]=
{
    "hawk",
    "eagle",
    "pigeon",
    "sparrow",
    "cardinal",
    "raven",
    "pelican"
};

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.

#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",
        "eagle",
        "pigeon",
        "sparrow",
        "cardinal",
        "raven",
        "pelican"
    };

    char* color[ARRAY_SIZE]=
    {
        "red",
        "orange",
        "yellow",
        "green",
        "blue",
        "indigo",
        "violet"
    };

    char* car[ARRAY_SIZE]=
    {
        "Hyundai Tucson",
        "Ford Mustang",
        "Dodge Charger",
        "Ford Focus",
        "Chevrolet Tahoe",
        "Subaru Outback",
        "Buick Park Avenue"
    };

    char* sounds[ARRAY_SIZE]=
    {
        "barking",
        "meowing",
        "cooing",
        "roaring",
        "growling",
        "cawing",
        "squeaking"
    };

    char* song[ARRAY_SIZE]=
    {
        "\"don\'t stop believin\'\" by journey",
        "\"bohemian rhapsody\" by queen",
        "\"under the bridge\" by red hot chili peppers",
        "\"logan circle\" by the wonder years",
        "\"shout at the devil\" by motley crue",
        "\"do re mi\" by blackbear",
        "\"the time (dirty bit)\" by the black eyed peas"
    };

    char* number[ARRAY_SIZE]=
    {
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven"
    };

    char* endings[ARRAY_SIZE]=
    {
        "they all had a bonfire",
        "they all watched a movie",
        "they all went out to eat",
        "they all ate dinner at home",
        "they all watched the stars at night",
        "they all set off fireworks",
        "they all went to bed to continue visiting in the morning"
    };

    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 thirr 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.\n",number[rand()%ARRAY_SIZE], answers[NAME], answers[PET_NAME], endings[rand()%ARRAY_SIZE]);

    return 0;
}

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.

craig@Vera:~/C_Programs/Console/Story/bin/Release$ ./Story 
what is your name?
Craig
what is your favorite animal?
dog
where were you born?
Fargo
how old are you?
67
where do you currently live?
Horace
what do you want your future career to be?
retired person
what is your pet's name?
Lily
=================================================================
Flying through the sky, a pigeon looks down upon Horace
and spots Craig. Craig is 67 years old and is studying to
become a retired person. Craig is on thier way to Fargo
to visit their two family members. As Craig packs their stuff
into a violet Ford Focus, their pet dog, Lily gets in the vehicle,
and starts to barking. Craig pets Lily and gets in the vehicle.
Craig starts the car and turns the radio up. Craig and Lily listens to "the time (dirty bit)" by the black eyed peas
for the whole seven hour long trip. Once Craig and Lily arrived, they all had a bonfire.

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.