Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.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
在Struct中查找项并编辑它们_C_Pointers_Struct_Filtering_Arrow Functions - Fatal编程技术网

在Struct中查找项并编辑它们

在Struct中查找项并编辑它们,c,pointers,struct,filtering,arrow-functions,C,Pointers,Struct,Filtering,Arrow Functions,我的代码中有一个问题(案例2),我试图得到项目的位置,就像我从scanf()中得到的一样,但当我试图得到位置时,它会给我一个完整的随机数,比如537890。 我不明白为什么我的代码会这样做,因为我的结构的最大大小是200 我不确定这是否是因为我尝试了&find==wh[a]->artikel int main() { struct managementtool { char artikel[200]; int anzahl; }; //

我的代码中有一个问题(案例2),我试图得到项目的位置,就像我从scanf()中得到的一样,但当我试图得到位置时,它会给我一个完整的随机数,比如537890。 我不明白为什么我的代码会这样做,因为我的结构的最大大小是200

我不确定这是否是因为我尝试了
&find==wh[a]->artikel

int main() {

    struct managementtool {
        char artikel[200];
        int anzahl;
    };

    //wh = warehouse

        struct managementtool **wh = malloc(200 * sizeof(struct managementtool *));

        for (int i = 0; i < 200; i++) {
            wh[i] = malloc(sizeof(struct managementtool));
        }


    printf("Welcome to Warehouse Management 97\n\n\nWhat do you want to do ?\n");


    int x,v,f,i,exit,all,end,a,b;


    char ques,find, nu1;

    do {
        i=0;
        printf("\n(1)Add article\n(2)Edit article.\n(3)Search entry.\n(4)Show stock.\n(5)Exit\n");
        scanf("%x",&x);

        switch (x) {
            case 1://add
                do {

                    printf("\nEnter the product name:  ");
                    scanf("%s", wh[f]->artikel);

                    printf("\nAmount of products:  ");
                    scanf("%i", &wh[f]->anzahl);

                    printf("\n\nAdd another product ? (Y/N)");
                    // add a space before % to skip leading whitespace
                    scanf(" %c", &ques);
                    f++;
                    switch (ques) {
                        case 'Y':
                            v++;
                            break;
                        case 'N':
                            end = 1;
                            v = 0;
                            break;
                        default:
                            printf("Wrong entry\n");
                            break;
                    }
                } while (end != 1);
                if (v >= 2) {
                    printf("Product added successfully\n\n");
                }else {
                    printf("Products have been successfully added\n\n");
                }
                break;

            case 2://edit
                printf("Which article do you want to edit?");
                fflush(stdin);
                scanf("%s", &find);
                for (a=0;a<f;a++) {
                    if (&find==wh[a]->artikel) {
                        b=a;
                    }
                }
                if (b==0) {
                    printf("Article not found");
                }
                printf("f: %i, b:%i",f,b);
                puts(wh[b]->artikel);
                printf("Amount: %d\n", wh[b]->anzahl);

                break;
            case 3://search
                break;
            case 4://Spam-it

                while (i<f) {
                    printf("\nProduct number %i\n", i+1);
                    printf("Name: ");
                    puts(wh[i]->artikel);
                    printf("Amount: %d\n", wh[i]->anzahl);
                    i++;
                }
                printf("\nTotal amount of Items: %i", all);
                break;
            case 5://go away
                printf("Goodbye :)");
                exit=1;
                break;
            default://well
                printf("Wrong Input\n");
                break;
        }
        all=0;
        while (i<f) {

            all += wh[i]->anzahl;
            i++;
        }
    } while (exit==0);
}
intmain(){
结构管理工具{
char-artikel[200];
国际安扎尔;
};
//仓库
结构管理工具**wh=malloc(200*sizeof(结构管理工具*);
对于(int i=0;i<200;i++){
wh[i]=malloc(sizeof(struct managementtool));
}
printf(“欢迎使用仓库管理97\n\n\n您想做什么?\n”);
int x,v,f,i,exit,all,end,a,b;
char-ques,find,nu1;
做{
i=0;
printf(“\n(1)添加文章\n(2)编辑文章。\n(3)搜索条目。\n(4)显示库存。\n(5)退出\n”);
scanf(“%x”、&x);
开关(x){
案例1://添加
做{
printf(“\n输入产品名称:”);
scanf(“%s”,wh[f]->artikel);
printf(“\n产品数量:”);
scanf(“%i”&wh[f]>anzahl);
printf(“\n\n添加其他产品?(是/否)”);
//在%之前添加空格以跳过前导空格
scanf(“%c”和“&ques”);
f++;
开关(ques){
案例“Y”:
v++;
打破
案例“N”:
结束=1;
v=0;
打破
违约:
printf(“错误输入\n”);
打破
}
}while(end!=1);
如果(v>=2){
printf(“产品添加成功\n\n”);
}否则{
printf(“产品已成功添加\n\n”);
}
打破
案例2://编辑
printf(“您想编辑哪篇文章?”);
fflush(stdin);
scanf(“%s”,查找(&F));
对于(a=0;aartikel){
b=a;
}
}
如果(b==0){
printf(“未找到物品”);
}
printf(“f:%i,b:%i”,f,b);
put(wh[b]>artikel);
printf(“金额:%d\n”,wh[b]->anzahl);
打破
案例3://搜索
打破
案例4://垃圾邮件
while(iartikel);
printf(“金额:%d\n”,wh[i]->anzahl);
i++;
}
printf(“\n项目总数:%i”,全部);
打破
案例五:走开
printf(“再见:)”;
退出=1;
打破
default://well
printf(“输入错误”);
打破
}
all=0;
而(安扎尔,;
i++;
}
}而(出口==0);
}

在C语言中,您需要使用strcmp而不是==来比较字符串。您还应该使用fgets而不是scanf。这将使您的生活更轻松,您不必担心尝试刷新。例如:fgets(我的字符串缓冲区,字符数,最大读取数,stdin)。strcmp示例:if(!strcmp(string1,string2),如果它们相同,strcmp将返回0。

scanf(“%s”,&find)
。请阅读scanf手册以了解
%s
的含义。它需要一个字符数组,并且您已经给了它一个指向单个字符的指针。此外,始终检查函数的返回值。特别是,在继续使用解析结果之前,检查
scanf
的结果非常重要。请注意,传递在C规范中,只有流到
fflush
(例如
fflush(stdin)
)的nput才被明确提到为未定义的行为。一些编译器将其添加为不可移植且非常非标准的扩展,请不要使用它。为什么要使用
scanf(“%x”,&x)
?您似乎不太确定如何使用
scanf
。也许您应该后退几步,刷新课堂笔记、教程或课本?至于
&find==wh[a]->artikel本身,我假设您想比较两个字符串,看看它们是否相等。表达式不是这样做的,它将比较两个指针,它们永远不会相等。再次,请后退几步,刷新课堂笔记、教程和教科书。