4

No matter how I edit my program there seems to be overflow errors and mismatching type errors. Can someone help me to make this run without errors.

#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: ");
    }
}

Any help is greatly appreciated and I would love an explanation as to what I did wrong so that I can learn and not make the same mistakes next time.

4

2 回答 2

2

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

您的代码中的错误:

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

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

你的 scanf 应该看起来像scanf("%s", type);

于 2013-10-08T18:55:00.927 回答
2

声明typecharacter array

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];
于 2013-10-08T18:58:00.533 回答