首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C中由strcmp和strcpy引起的程序错误

C中由strcmp和strcpy引起的程序错误
EN

Stack Overflow用户
提问于 2013-10-08 18:49:08
回答 2查看 730关注 0票数 4

无论我如何编辑我的程序,似乎都存在溢出错误和错误匹配类型错误。有人能帮我顺利完成这件事吗。

代码语言:javascript
复制
#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: ");
    }
}

任何帮助都是非常感谢的,我希望能解释一下我做错了什么,这样我就可以学习,下次也不会犯同样的错误。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-08 18:58:00

声明typecharacter array

代码语言:javascript
复制
char type[50];

删除&中的scanf()。读取字符串时不应使用&

代码语言:javascript
复制
   scanf("%s", &type); ==>   scanf("%s", type);
               ^  

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

代码语言:javascript
复制
  strcpy(donations_amount[i], donations_amount[i + 1]);  
  strcpy(req_amount[i], req_amount[i + 1]);   

像这样修改

代码语言:javascript
复制
 donations_amount[i]=donations_amount[i + 1];
 req_amount[i]= req_amount[i + 1];
票数 2
EN

Stack Overflow用户

发布于 2013-10-08 18:55:00

而不是char type,您需要char type[100]

代码中的错误:

代码语言:javascript
复制
if (strcmp(donations_inv_type[i], type) == 0)
 //                               ^^^^ should be char*

注意:函数strcmp()strcpy()应该传递以\0以nul结尾的char数组(或者说字符串)。

你的扫描应该像scanf("%s", type);

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19255840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档