Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
询问用户';在C中循环多次输入_C_Pointers_Input_Struct_While Loop - Fatal编程技术网

询问用户';在C中循环多次输入

询问用户';在C中循环多次输入,c,pointers,input,struct,while-loop,C,Pointers,Input,Struct,While Loop,在这个初学者代码中,我试图要求用户输入,只要输入不是“退出”。我的代码: void createReport(){ printf("Welcome to the report generator, type item's ID: "); char *userInput; int i=0; struct myStruct{ char *name; int id; float sale; };

在这个初学者代码中,我试图要求用户输入,只要输入不是“退出”。我的代码:

void createReport(){
    printf("Welcome to the report generator, type item's ID: ");
    char *userInput;
    int i=0;
    struct myStruct{
        char *name;
        int id;
        float sale;
    };
    struct myStruct *myStructArray[MAX_ITEMS];
    fgets(userInput,sizeof(int),stdin);
    while(strcmp(userInput,"quit")!=0) {
        userInput = malloc(sizeof(int));
        fgets(userInput,sizeof(int),stdin);
        searchA(userInput); //different function that changes global variable deletion
        printf("Added to the report, add more or type \'quit\' \n");
        strcpy(myStructArray[i]->name,inventoryItem[deletion].name);  //inventoryItem is global
        myStructArray[i]->id = atoi(input);
        myStructArray[i]->sale = inventoryItem[deletion].sale;
        i++;
        free(userInput);
    }
    for(int x=0;x<30;x++) printf(myStructArray[x]->name);  //never executed
}
void createReport(){
printf(“欢迎使用报告生成器,键入项目ID:”);
字符*用户输入;
int i=0;
结构myStruct{
字符*名称;
int-id;
浮动销售;
};
结构myStruct*myStructArray[MAX_ITEMS];
fgets(userInput、sizeof(int)、stdin);
while(strcmp(用户输入,“退出”)!=0){
userInput=malloc(sizeof(int));
fgets(userInput、sizeof(int)、stdin);
searchA(userInput);//更改全局变量删除的不同函数
printf(“添加到报告中,添加更多或键入'quit\'\n”);
strcpy(myStructArray[i]->name,inventoryItem[deletation].name);//inventoryItem是全局的
myStructArray[i]->id=atoi(输入);
myStructArray[i]->sale=inventoryItem[删除].sale;
i++;
免费(用户输入);
}
for(int x=0;xname);//从未执行
}
此代码运行一次,请求用户输入,然后完成


有人知道哪里会出错吗?感谢您在执行
循环之前提供的任何建议

fgets(userInput,sizeof(int),stdin);
此时,尚未设置变量
userInput

编辑: 代码思想的运行示例:

printf("Welcome to the report generator, type item's ID: ");
const int input_size = 6 * sizeof(char);
char *userInput = malloc(input_size);
int i = 0;
struct myStruct {
    char *name;
    int id;
    float sale;
};
struct myStruct *myStructArray[MAX_ITEMS];

fgets(userInput, input_size, stdin);
while (strcmp(userInput, "quit") != 0 && strcmp(userInput, "quit\n") != 0) 
{
    // do whatever you like with myStructArray[i], for example:
    myStructArray[i] = malloc(sizeof(struct myStruct));
    myStructArray[i]->name = malloc(sizeof(char));
    myStructArray[i]->name = "ab";
    myStructArray[i]->id = 7;
    myStructArray[i]->sale = 99.9F;
    printf("Added %s, %d, %f\n", myStructArray[i]->name, myStructArray[i]->id, myStructArray[i]->sale);

    //prepare for next iteration:
    printf("Added to the report, add more or type \'quit\' \n");
    for (int j = 0; j < input_size; ++j) // zeroing allocated data on the heap
        userInput[j] = 0;

    fgets(userInput, input_size, stdin);
    i++;
}
free(userInput);
for (int x = 0; x < i; x++) printf("%s,", myStructArray[x]->name);  
printf(“欢迎使用报告生成器,键入项目ID:”);
const int input_size=6*sizeof(字符);
char*userInput=malloc(输入大小);
int i=0;
结构myStruct{
字符*名称;
int-id;
浮动销售;
};
结构myStruct*myStructArray[MAX_ITEMS];
fgets(用户输入、输入大小、标准输入);
while(strcmp(userInput,“quit”)!=0和strcmp(userInput,“quit\n”)!=0)
{
//使用myStructArray[i]执行您喜欢的任何操作,例如:
myStructArray[i]=malloc(sizeof(struct myStruct));
myStructArray[i]->name=malloc(sizeof(char));
myStructArray[i]->name=“ab”;
myStructArray[i]->id=7;
myStructArray[i]>sale=99.9F;
printf(“添加了%s,%d,%f\n”,myStructArray[i]->name,myStructArray[i]->id,myStructArray[i]->sale);
//为下一次迭代做准备:
printf(“添加到报告中,添加更多或键入'quit\'\n”);
for(int j=0;jname);

fgets
strcmp
一起使用时,您的
userInput
无效。另外
userInput=malloc(sizeof(int))sizeof(int)
为4个字节,code>将不足以容纳
quit
。是的,我没有考虑到这一点,比如说我分配了足够的内存,但你说的无效是什么意思?我有
char*userInput
,这还不够吗?
*userInput
此时还没有初始化。另一个问题是,在每个循环中为它分配内存,这可能只是一种浪费。我将编辑我的答案,为您提供代码的固定版本。因此,我尝试调试代码,但在执行
strcpy(myStructArray[I]->name,inventoryItem[deletation].name)时出现了分段错误我想这可能是问题所在,我猜不出使用我看不到的变量的问题所在。如果你把整个相关代码,包括:
inventoryItem
删除
搜索(…)
,我可以看到问题所在,并提出一个好的解决方案。