C语言中因strcmp和strcpy而导致程序错误

4
无论我如何编辑我的程序,似乎总会出现溢出错误和类型不匹配的错误。有人可以帮我使其运行时没有错误吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    int choice;
    int i;
    int j;
    char type;
    int amount;
    int don_count = 0;
    int req_count = 0;
    int flag;
    char donations_inv_type[100][20];
    int donations_amount[100];
    char requests_inv_type[100][20];
    int req_amount[100];

    printf("Welcome to the Food Bank Program\n\n  1.  Add a donation\n  2.  Add a request\n  3.  Fulfill a request\n  4.  Print status report\n  5.  Exit\n\nEnter your choice: ");
    scanf("%d", &choice);

    while (choice != 5) {
        if (choice == 1) {
            printf("\nEnter inventory type: ");
            scanf("%s", &type);
            printf("Enter the amount: ");
            scanf("%d", &amount);
            printf("\nDonation Added!\n\n");
            flag = -99;
            for (i = 0; i < don_count; i++) {
                if (strcmp(donations_inv_type[i], type) == 0)
                    flag = i;
            }
                if (flag == -99) {
                    strcpy(donations_inv_type[i], type);
                    donations_amount[i] = amount;
                    don_count++;
                }
                else
                    donations_amount[flag] += amount;
        printf("Donation Added!\n");
        printf("Press any key to continue . . .\n\n");
        }
        else if (choice == 2) {
            printf("\nEnter inventory type: ");
            scanf("%s", &type);
            printf("Enter the amount: ");
            scanf("%d", &amount);
            strcpy(requests_inv_type[req_count], type);
            req_amount[req_count] = amount;
            req_count++;
        }
        else if (choice == 3) {
            printf("\n\n-------- Fulfilling Requests--------");
            flag = -99;
            for (i = 0; i < don_count; i++) {
                if (strcmp(donations_inv_type[i], requests_inv_type[0]) == 0)
                    flag = i;
            }
            if (flag == -99)
                printf("Cannot be Fulfilled\n\n");
            else if (donations_amount[flag] > req_amount[0]) {
                donations_amount[flag] -= req_amount[0];
                printf("Request Fulfilled");
                req_amount[0] = 0;
            }
            else if (donations_amount[flag] == req_amount[0]) {
                printf("Request Fulfilled");
                for (i = flag; i < don_count; i++) {
                    strcpy(donations_inv_type[i], donations_inv_type[i + 1]);
                    strcpy(donations_amount[i], donations_amount[i + 1]);
                }
                don_count--;
                for (i = flag; i < req_count; i++) {
                    strcpy(requests_inv_type[i], requests_inv_type[i + 1]);
                    strcpy(req_amount[i], req_amount[i + 1]);
                }
                req_count--;
            }
            else if (donations_amount[flag] < req_amount[0]) {
                printf("Partially Fulfilled");
                req_amount[0] -= donations_amount[flag];
                for (i = flag; i < don_count; i++) {
                    strcpy(donations_inv_type[i], donations_inv_type[i + 1]);
                    strcpy(donations_amount[i], donations_amount[i + 1]);
                don_count--;
            }
            }
        }
        else if (choice == 4) {
            printf("Printing the Donations Table\n\n");
            for (i = 0; i < don_count; i++) {
                printf("%s  %d", donations_inv_type[i], donations_amount[i]);
            }
            printf("Printing the Requests Table\n\n");
            for (i = 0; i < req_count; i++) {
                printf("%s  %d", requests_inv_type[i], req_amount[i]);
            }
        }
        printf("Welcome to the Food Bank Program\n\n  1.  Add a donation\n  2.  Add a request\n  3.  Fulfill a request\n  4.  Print status report\n  5.  Exit\n\nEnter your choice: ");
    }
}

非常感谢您的帮助,希望您能解释一下我做错了什么,并让我学习,以便下次不犯同样的错误。

"type" 应该是 "char*",而不是 "char",因此 "strcmp(donations_inv_type[i], type)" 是未定义的行为。 - Grijesh Chauhan
@GrijeshChauhan,不仅如此,它还需要是一个数组,因为OP要扫描到它。 - Charlie Burns
2个回答

2

type声明为字符数组

char type[50];

scanf()中去掉&。读取字符串时不应使用&

   scanf("%s", &type); ==>   scanf("%s", type);
               ^  

在这里,您需要复制整数而不是字符串。

  strcpy(donations_amount[i], donations_amount[i + 1]);  
  strcpy(req_amount[i], req_amount[i + 1]);   

修改为:

 donations_amount[i]=donations_amount[i + 1];
 req_amount[i]= req_amount[i + 1];

我已经进行了这些更正,程序可以运行,但在我运行选项一后告诉我它停止工作了。 - computersciencestudent
1
是的,可能还有许多其他错误。我们只为最明显的一个提供了答案。 - Charlie Burns
@computersciencestudent 非常欢迎。对于这种情况,您只需添加一个带有名称和一些金额的捐赠。请使用调试器进行试运行。您会知道的。询问所有事情确实不是一件好事。 - Gangadhar

2

你需要使用 char type[100] 而不是 char type

你的代码存在错误:

if (strcmp(donations_inv_type[i], type) == 0)
 //                               ^^^^ should be char*

注意:函数 strcmp()strcpy() 应该传递以 \0 结尾的字符数组(或称字符串)。
您的 scanf 应该像这样:scanf("%s", type);

当我将其读入字符串时,它不会自动变为 null 终止吗? - computersciencestudent
1
@computersciencestudent 当你使用scanf()函数时,字符串会自动变成以空字符结尾的字符串。但是有些人错误地使用了这个函数。 - Grijesh Chauhan
我将[type]转换为数组,并将其扫描到字符数组中,但似乎仍然无法正常工作。 - computersciencestudent
可能还有许多其他错误,我们只评论了最严重的一个。你可能需要运用一些调试技巧。 - Charlie Burns
@Grijesh Chauhan C 规范 5.2.1 将 \0 称为“空字符”。ASCII 则将代码为 0 的字符称为“NUL”。 - chux - Reinstate Monica
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接