为什么我的字符串没有打印在我的故事中

问题描述 投票:0回答:1

我的教授要求我们使用用户输入的个人信息以及 srand() 和 rand() 函数创建一个故事。

这是我写的代码。我希望故事被写出来,%s 被填写,但其中一些是多个字符串类型,没有空格,还有一些是“(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;

arrays c srand
1个回答
0
投票

在分析代码时,简短的答案是您的各种文字数组需要用逗号分隔每个字符串集。

例如,在您的代码中,“birds”数组是这样的:

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

但应该像下面这样:

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

当我尝试不使用分隔符的代码时,我得到了一堆“空”详细信息。此外,正如评论中所指出的,“获取”调用似乎没有理由。考虑到这一点,以下是使用各种逗号分隔的字符串值重构的代码。

#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;
}

将整个重构的代码放入这个答案中会使其变得冗长,但我觉得查看它很重要。修改代码后,以下是测试运行的终端输入和输出。

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.

回顾一下,了解字符数组需要如何初始化。也许您可能想深入研究一些教程文献,以更好地了解数组,尤其是字符数组。

© www.soinside.com 2019 - 2024. All rights reserved.