Question

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.

Was it helpful?

Solution

Declare type as character array.

char type[50];

Remove & in scanf(). You should not use & while reading string.

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

Here you want to copy integers not strings

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

Modify like this

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

OTHER TIPS

Instead of char type you need char type[100]

Error in your code:

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

Note: Functions strcmp() and strcpy() should be passed \0 nul-terminated array of char (or say string).

Your scanf should look like scanf("%s", type);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top