【发布时间】:2021-04-03 11:57:24
【问题描述】:
我想请用户向我提出 2 或 3 个不同类别的一系列问题。然后我想在 2 或 3 个不同的类别中打印出这些相同的问题。我在 for 循环中使用了 for 循环,但是当我打印时,系统只打印出最新 for 循环中的问题。提前谢谢你。
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int tiers;
char *question [81]; // pointers to binary questions
int num[81]; // number of binary questions to be asked
printf("How many tiers do you have? For example if you have high, medium and low categories, you
have three tiers.\n");
scanf("%i", &tiers);
for (int i = 0; i < tiers; ++i)
{
printf("You are in tier number %i\n\n", i+1);
printf("You will be asked to write a series of binary questions.\n");
printf("How many questions are you going to ask?\n");
scanf("%i", &num[i]);
getchar();
for (int j =0; j<num[i]; ++j)
{
question[j] = malloc(sizeof(char)*584);
printf("What is question number %i\n", j+1);
fgets(question[j], 584, stdin);
}
printf("\n\n\n");
}
/* From here the user is now a different person */
//asking questions in the respective tiers
for (int i = 0; i < tiers; ++i)
{
printf("You are in tier number %i\n", i+1);
printf("I am now going to ask you a series of binary questions.\n");
for ( int j =0; j <num[i]; ++j)
{
printf("\n%s", question[j]);
}
}
for (int i = 0; i < tiers; ++i)
{
for (int j =0; j<num[i]; ++j)
{
free (question[j]);
}
}
return 0;
}
【问题讨论】: