0

我正在一个程序中工作,该程序必须读取用户输入(姓氏、名字和分数)并将这些信息写入文本文件。我创建了一个循环,提示输入并为每个输入权设置标志。当收集所有输入并且用户不想添加任何其他内容时,循环应该结束。

问题是我设置了变量来保存名称,但我需要一种方法来重用这些变量以用于循环的下一次迭代。因此,循环开始,收集用户输入,写入文件,然后它应该“清除”新迭代的变量。

用户应该能够在文件中插入尽可能多的数据。但现在,我只需添加一组数据:(姓氏、名字和分数)。

void enter_data(void)
{

/* create file */
    FILE *fp;

    /*variables*/
    char answer;
    int flag1 = 0;
    int flag2 = 0;
    int flag3 = 0;
    int score = 0;  
    int test;
    char leave[SIZE] = "exit";
    char input_lname[SIZENAME];
    char input_fname[SIZENAME];
    char input_score[5];
    char input_answer[5];
    char lname[SIZENAME];
    char fname[SIZENAME];




    /*check if file was created */
    if((fp = fopen("students.dat", "ab+")) == 0){
        perror("fopen");
    }


    /* start of interaction */
    while(!(flag1 && flag2 && flag3))
    {
        flag1 = 0;
        flag2 = 0;
        flag3 = 0;

        /* ask for last name */
        while(flag1 != 1)
        {
            printf("Enter last name:\n");

            if(!fgets(input_lname, SIZENAME, stdin))
            {
                clearerr(stdin);
                break;
            }   
            /* validate last name. if not digit... */
            if(!sscanf(input_lname, "%d", &test) == 1)

                /*put first word in lname */
                sscanf(input_lname, "%16s", lname);

            if(strcmp(input_lname, leave) != 0)
            {
                flag1 = 1;
                break;
            }
        }


        /* ask for first name */
        while(flag2 != 1)
        {
            printf("Enter first name:\n");

            if(!fgets(input_fname, SIZENAME, stdin))
            {
                clearerr(stdin);
                break;
            }   
            /* validate first name. if not digit... * */
            if(!sscanf(input_fname, "%d", &test ) == 1)
            {
                /*put first word in fname */
                sscanf(input_fname, "%16s", fname);

                flag2 = 1;
                break;
            }
        }


        /* ask for score */
        while(flag3 != 1)
        {
            printf("Enter score:\n");

            if(!fgets(input_score, SIZENAME, stdin))
            {
                clearerr(stdin);
                break;
            }

            /* validate last name */
            if(sscanf(input_score, "%3d", &score) == 1)
            {
                if((score >= 0) && (score <= 100))
                {
                    flag3 = 1;
                    break;
                } else if (score == -1)
                    break;
            }
        }

        /* store record in file */
        if((flag1 == 1) && (flag2 == 1) && (flag3 == 1))
        {
            printf("r %16s, %16s, %5d", lname, fname, score);
            fprintf(fp, "%16s %16s %5d", lname, fname, score);


        }

        /* ask for another student */
        printf("Would you like to enter data for another student?\n");
        if(!fgets(input_answer, SIZE, stdin))
        {
            clearerr(stdin);
            break;
        }   
        if(sscanf(input_answer, "%s", &answer)  == 1)
        {
            if(answer == 'y') 
            {
                flag1 = 0;
                flag2 = 0;
                flag3 = 0;
                continue;
            } else
                break;
        }

        flag1 = 0;
        flag2 = 0;
        flag3 = 0;

        fclose(fp);
        /* finish asking */

    }


}
4

1 回答 1

1
   struct Record {
    /*variables*/
    char answer;
    int flag1;
    int flag2;
    int flag3;
    int score;  
    int test;
    char leave[SIZE];
    char input_lname[SIZENAME];
    char input_fname[SIZENAME];
    char input_score[5];
    char input_answer[5];
    char lname[SIZENAME];
    char fname[SIZENAME];
   }

   while ( /* condition */ ) {
       // Create a record to keep the user inputs.
       Record oneRecord;
       oneRecord.flag1 = 1;
       oneRecord.flag2 = 2;
       ....

       // Write to file or other else.
   }

'struct' 语法可能会对您有所帮助。

于 2013-10-11T07:31:09.850 回答