【问题标题】:Is it possible to store data within 2 for-loops是否可以在 2 个 for 循环中存储数据
【发布时间】: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;
}

【问题讨论】:

    标签: arrays c string for-loop


    【解决方案1】:

    您对所有类别使用相同的数组,因此只保存最后一个类别的输入。

    您必须使用单独的数组来存储所有输入。

    在此代码中,question 是 char* 的 81 个 81 元素数组,而不是原始代码中 char* 的单个 81 元素数组。

    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
        int tiers;
        char *question [81][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[i][j] = malloc(sizeof(char)*584);
                printf("What is question number %i\n", j+1);
    
                fgets(question[i][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[i][j]);
                }
        }
    
        for (int i = 0; i < tiers; ++i)
        {
                for (int j =0; j<num[i]; ++j)
                {
                        free (question[i][j]);
                }
        }
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      相关资源
      最近更新 更多