C语言故事生成程序字符串打印异常问题求助
问题分析与修复方案
你的代码出现无空格拼接字符串和**(NULL)占位符**的问题,核心是以下几个错误导致的:
1. 字符串数组元素缺少分隔逗号
所有字符串数组(bird、color、car等)里的元素之间没加逗号,C语言会自动把相邻字符串常量拼接成一个长串,导致数组实际有效元素数量远少于ARRAY_SIZE,剩余位置被初始化为NULL。当rand()取到这些NULL位置时,就会显示(NULL),而拼接后的长串会造成无空格填充的问题。
修复方法:给每个数组元素末尾加上逗号(最后一个元素可省略,但建议统一格式),示例:
char* bird[ARRAY_SIZE]={ "hawk", //0 "eagle", //1 "pigeon", //2 "sparrow", //3 "cardinal", //4 "raven", //5 "pelican" //6 };
所有类似的字符串数组都需要做此修改。
2. 输入处理逻辑错误
你先用fgets()读取用户输入,紧接着又用gets()覆盖内容,这会引发三个问题:
fgets()读取的内容直接被丢弃,gets()会读取fgets()留下的换行符,导致answers[i]变成空字符串gets()是不安全函数,存在缓冲区溢出风险- 若
answers[i]为空,strlen(answers[i])-1会访问数组越界,触发未定义行为
修复方法:删掉gets()调用,仅保留fgets(),并处理掉fgets()读取到的换行符:
for(int i=0; i<ARRAY_SIZE; i++){ printf("%s\n", questions[i]); if(fgets(answers[i],MAX_LEN,stdin) == NULL){ fprintf(stderr, "输入错误\n"); return 1; } // 去除换行符 size_t len = strlen(answers[i]); if(len > 0 && answers[i][len-1] == '\n'){ answers[i][len-1] = '\0'; } }
3. 拼写错误
故事文本里的on thier way是拼写错误,需改为on their way,避免输出错误内容。
修复后的完整代码
#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]); if(fgets(answers[i],MAX_LEN,stdin) == NULL){ fprintf(stderr, "输入错误\n"); return 1; } size_t len = strlen(answers[i]); if(len > 0 && answers[i][len-1] == '\n'){ answers[i][len-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 their 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; }
内容的提问来源于stack exchange,提问作者BluemanDoug
相关产品推荐
相关产品推荐

