如何在C语言中重复输入文本的流程?

如何在C语言中重复输入文本的流程?,c,function,C,Function,我做了一个循环,不让一切重复一遍又一遍。但无论何时我试图输入我的名字,它都不允许我和进程取消。如何做到这一点 int new_acc(FILE * fp, char *name, size_t namesize, char *dob, size_t dobsize){ int one_by_one; char listing[8][15] = {"Name","Date of birth"}; char another_list[8][15] = {"name","do

我做了一个循环,不让一切重复一遍又一遍。但无论何时我试图输入我的名字,它都不允许我和进程取消。如何做到这一点

int new_acc(FILE * fp, char *name, size_t namesize, char *dob, size_t dobsize){
    int one_by_one;
    char listing[8][15] = {"Name","Date of birth"};
    char another_list[8][15]  = {"name","dob"};         //   These two
    char list_size[8][15] = {"namesize","dobsize"};    //    lines are having problem.

    for (int i=0; i<one_by_one; i++){
        printf("Enter your %s: ",listing + i);
        fgets(another_list + i, list_size, stdin);
    }

    /* This is without loop printing */
    printf("Enter your name: ");
    fgets(name, namesize, stdin);
    fputs(name, fp);

    printf("Enter your Date of Birth: ");
    fgets(dob, dobsize, stdin);
    fputs(dob, fp);

    fclose(fp);

    return 0;
}

这个数组有8行14+空终止符的空间,所以在设置'ono_by_一个变量时必须小心,顺便说一句,这个变量是未初始化的,这也是一个问题

应使用大于1且小于8的值对其进行初始化

也不需要对其进行初始化,因为要覆盖存储在其中的字符串,请执行以下操作:

char another_list[8][15];
这一行:

fgets(another_list + i, list_size, stdin);
printf("Enter your %s: ",listing + i);
不正确,第二个参数要求读取字符串的大小,该大小不应大于存储该字符串的容器,因此它应该类似于:

fgets(another_list[i], sizeof(another_list[i]), stdin);
这一行:

fgets(another_list + i, list_size, stdin);
printf("Enter your %s: ",listing + i);
因为清单只有两行,所以一行接一行应该不大于2行,所以没有多大意义。它将在第一次迭代中打印姓名,在第二次迭代中打印出生日期,然后在内存中不打印任何内容或任何它将从中读取的内容,这是未定义的行为

因此,整个代码应该是:

int new_acc(FILE * fp, char *name, size_t namesize, char *dob, size_t dobsize){

    int one_by_one = 8;
    char listing[][15] = {"Name","Date of birth"}; //the number of line can be ommited
    char another_list[8][15];
    char list_size[8][15] = {"namesize","dobsize"};

    for (int i=0; i< one_by_one; i++){
        printf("Enter your %s: ",listing + i); // needs to be changed
        fgets(another_list[i], sizeof(another_list[i]), stdin);
    }

    //here I can't help since I don't know the state of the arguments you 
    //will be passing to the funnction
    printf("Enter your name: ");
    fgets(name, namesize, stdin);
    fputs(name, fp);

    printf("Enter your Date of Birth: ");
    fgets(dob, dobsize, stdin);
    fputs(dob, fp);

    fclose(fp);

    return 0;
}