C 使用指针和数组处理字符串创建随机故事生成器

C 使用指针和数组处理字符串创建随机故事生成器,c,arrays,pointers,c-strings,srand,C,Arrays,Pointers,C Strings,Srand,我正试图弄清楚我应该如何为编程课构建我的下一个实验室。这是我到目前为止所拥有的。问题逐行列出: int main() { char name, color, person; //color, pet and car are the only arrays. The rest will be read in int age; const char* pet[5]={"dog", "cat", "bird", "snake", "monkey"}; //not

我正试图弄清楚我应该如何为编程课构建我的下一个实验室。这是我到目前为止所拥有的。问题逐行列出:

int main()
{       
    char name, color, person; //color, pet and car are the only arrays. The rest will be read in
    int age;

    const char* pet[5]={"dog", "cat", "bird", "snake", "monkey"}; //not sure how to set up these pointers...
    const char* car[5]={"porsche 911", "honda prelude", "toyota prius", "shelby mustang"};

    printf("What is your name?   \n");
    scanf("%c", &name);
    printf("How old are you?  \n");
    scanf("%d", &age);
    printf("What is your favorite color?  \n");
    scanf("%c", &color);
    printf("What is the name of your best friend?  \n");
    scanf("%c", &person);


    printf("%c is an awesome person.\n", name);
    printf(" They are currently %d years old and drive a %c %s.\n", age, color, car[5]); // trying to reference array in text
    printf(" %c 's best friend, %c, picks them up in a %s and drives them over to see their pet %s", name, person, car[5], pet[5]); // trying to reference array in text

    pet[5]=srand(time(NULL));// Where should this go? do I need one to reference each array?
    car[5]=srand(time(NULL));

    system("Pause");
    return 0;
}
更新时间4:17 10/22。我现在收到的唯一编译错误是将数组与srand函数关联起来

存在许多问题

  • name
    color
    person
    应该是
    char
    数组,而不是
    char
  • car[5]
    pet[5]
    的索引超出范围
  • pet[5]=srand(时间(空))我不知道你想用这个实现什么。这没有任何意义。你应该更新你的问题并详细说明
到目前为止已更正代码:

int main()
{       
    char name[50], color[50], person[50]; //color, pet and car are the only arrays. The rest will be read in
    int age;

    const char* pet[5]={"dog", "cat", "bird", "snake", "monkey"}; //not sure how to set up these pointers...
    const char* car[5]={"porsche 911", "honda prelude", "toyota prius", "shelby mustang"};

    printf("What is your name?   \n");
    scanf("%s", name);
    printf("How old are you?  \n");
    scanf("%d", &age);
    printf("What is your favorite color?  \n");
    scanf("%s", color);
    printf("What is the name of your best friend?  \n");
    scanf("%s", person);

    printf("%s is an awesome person.\n", name);
    printf(" They are currently %d years old and drive a %s %s.\n", age, color, car[4]); // trying to reference array in text
    printf(" %s 's best friend, %s, picks them up in a %s and drives them over to see their pet %s", name, person, car[3], pet[4]); // trying to reference array in text

    //no clue what this should be
    //pet[5]=srand(time(NULL));// Where should this go? do I need one to reference each array?
    //car[5]=srand(time(NULL));

    system("Pause");
    return 0;
}

您可能需要在字符串周围加引号,如“dog”、“cat”等。你试过编译这个吗?您还试图为宠物在大小为4的数组中放入5个值。
char[4]=srand(time(null))。。。什么?不能输入多个字符作为
名称
,以及其他字符串;而
%c
对于打印字符串没有用处<代码>汽车[5]
超出范围(宠物[5]
也超出范围)。这些,以及您对
srand
的奇怪使用,表明您可能需要在重试之前阅读标准手册或书籍。(你甚至可以找到另一种方法来替代在许多低质量问题中出现的那句咒骂的话
系统(“暂停”);
。)