我的教授要求我们使用用户输入的个人信息和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;
1条答案
按热度按时间pbwdgjma1#
在分析代码时,简短的回答是,您的各种文字数组需要将每个字符串设置为用逗号分隔。
例如,在你的代码中,“birds”数组是这样的:
但应该像下面这样:
当我尝试你的代码没有分隔符,我得到了一堆“空”的细节。此外,正如评论中所指出的,似乎没有理由“得到”调用。考虑到这一点,下面是使用各种逗号分隔的字符串值重构的代码。
将整个重构的代码放在这个答案中会使它变长,但我觉得查看它很重要。修改代码后,下面是用于测试运行的终端输入和输出。
概括一下,要认识到字符数组需要如何初始化。也许您可能想深入研究一些教程文献,以更好地理解数组,特别是字符数组。