5 Replies - 4710 Views - Last Post: 07 March 2014 - 09:27 PM Rate Topic: -----

#1 damatuse   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 14

Help with food bank c programming assignment

Posted 07 March 2014 - 11:15 AM

I am stuck and my program won't run. If you could help me out it would be greatly appreciated.
Here are the instructions:

COP 3223 Spring 2014 – Section 2
Introduction to C - Programming Assignment #3
Section 1 Due date: Friday, Feb 28, 2014


Objectives

1. To reinforce your knowledge of ‘for’ loops and ‘while’ loops
2. To utilize strings (character arrays) in C
3. To implement logic in a program that is menu-based and is the seed for a complex, practical software system.

Problem: Food Bank Program

A Food Bank is a non-profit organization which distributes food items to people in need during local emergency food programs. People come in to a Food Bank and make their donations, and others might come in to make a request for any food item they want.

A Food Bank Program is used to keep track of the stock the Food Bank has (i.e., the donations), keep a record of requests that come in, fulfilling requests and printing the status report which consists of the amount of stock present in the food bank and the requests that are not yet fulfilled.

The program would require you to make two tables: Donations Table and Request Table. Each table will have two attributes: the Inventory Type, which is what type of food item it is, e.g., Milk, Meat, etc., and the Amount, which is the amount of that food item, e.g., 20, 30, etc. Note: For simplification, no units for amounts are considered.

Functionality Specification

1. Add donations and requests to the tables.
2. For any new donation that comes in, check if any donation is present in the donations table with same inventory type; if there is one then increase its amount by the new donation amount, and do not add a new entry in the donations table. If not, then add a new entry in the donations table for that donation.
3. Remove a request from the request table when it is fulfilled.
4. If for a request the request amount is greater than the amount present, then partially fulfill that request by the amount present. And do not delete that request from the request table; just reduce its amount by the amount that is fulfilled.
5. If in the process of fulfilling the request, the amount present for any donation becomes zero, then remove that donation from the donations table.
6. Print the status report, containing stock currently available and requests that are not yet fulfilled.
7. When the option is to Fulfill a Request, you are to only process the one request that is at the top of the Request Table (the oldest request). Thus, if it cannot be fulfilled, it will stall the system, until someone donates enough, so it can be satisfied later.

Input/Output Specification

At the beginning, the program should present the user with the available options, and ask for his/her preference. Based on the preference, the program should perform the corresponding task.

The user will have five choices:
1. Enter a Donation
2. Enter a Request
3. Fulfill the earliest Request
4. Print status report
5. Exit

When the user selects ‘1’, the program should ask the user for the inventory type and amount of the donation to be made. When the donation has been added to the donations table, the program should notify the user by printing out “Donation Added”.

When the user selects ‘2’, the program should ask the user for the inventory type and amount of the request to be made. When the request has been added to the requests table, the program should notify the user by printing out “Request Added”.

When the user selects ‘3’, the program should print “Request Fulfilled” if the request is completely fulfilled, print “Cannot be Fulfilled” if there is no donation of that type, and print
“Partially Fulfilled” if the donation type exists, but cannot completely satisfy the request.
If a request is fulfilled, it should be deleted from the Requests Table; if a donation is fully
used up in satisfying a request, the Donation Table entry should be deleted.

When the user selects ‘4’, the program should print the current Donations Table and the Requests Table.

When the user selects ‘5’, the program should print “Thank You for running the software. Bye for now”, and then the program exits.


Implementation Hints

If we assume that the maximum number of donated types at any time is 100, and that the
Maximum length of a description of the type is 19, then, our tables can be declared as:
char donations_inv_type[100][20];
int donations_amount[100];
You will need to have variables to keep track (count) of the total donated types, and total requests, in the two tables.

Use function ‘strcmp’ to compare if two strings are equal. Remember that when the two strings are equal this function returns 0.

Use function ‘strcpy’ to copy from one string variable to another.

While removing the request from the requests table once it is fulfilled, you will have to shift up the requests that are below that request in the request table, to take the place of the removed request. The same applies when you remove a donation, if the donation amount becomes zero.

For every new donation you will have to check if the inventory type of that donation is present in the donations table or not. For this, use the ‘strcmp’ function.
For a new request that comes in, you do NOT have to perform the check for whether any request is present in the request table with the same inventory type. Just add a new line for each new request in the requests table, as every request corresponds to a different family.

Assume that all inputs are perfectly correct, so you don’t have to do any error checking on the input.

Output Sample

Welcome to the Food Bank Program

1. Add a donation
2. Add a request
3. Fulfill a request
4. Print status report
5. Exit

Enter your choice:_1_

Enter inventory type: MILK
Enter the amount: 20

Donation Added!
Press any key to continue . . .

Welcome to the Food Bank Program

1. Add a donation
2. Add a request
3. Fulfill a request
4. Print status report
5. Exit

Enter your choice:_2_

Enter inventory type: MILK
Enter the amount: 5

Request Added!
Press any key to continue . . .

Welcome to the Food Bank Program

1. Add a donation
2. Add a request
3. Fulfill a request
4. Print status report
5. Exit

Enter your choice:_1_

Enter inventory type: FRUIT
Enter the amount: 10

Donation Added!
Press any key to continue . . .

Welcome to the Food Bank Program

1. Add a donation
2. Add a request
3. Fulfill a request
4. Print status report
5. Exit

Enter your choice:_1_

Enter inventory type: MILK
Enter the amount: 5

Donation Added!
Press any key to continue . . .

Welcome to the Food Bank Program

1. Add a donation
2. Add a request
3. Fulfill a request
4. Print status report
5. Exit

Enter your choice:_4_

Printing the Donations Table

MILK 25
FRUIT 10

Printing the Requests Table

MILK 5

Press any key to continue . . .

Welcome to the Food Bank Program

1. Add a donation
2. Add a request
3. Fulfill a request
4. Print status report
5. Exit

Enter your choice:_3_

-------- Fulfilling Requests--------

Request Fulfilled

Press any key to continue . . .

Welcome to the Food Bank Program

1. Add a donation
2. Add a request
3. Fulfill a request
4. Print status report
5. Exit

Enter your choice:_4_

Printing the Donations Table

MILK 20
FRUIT 10

Printing the Requests Table


Press any key to continue . . .

Welcome to the Food Bank Program

1. Add a donation
2. Add a request
3. Fulfill a request
4. Print status report
5. Exit

Enter your choice:_5_


Thank You for using the software. Bye for now.

Press any key to continue . . .

References

Notes: Arrays, 2D Arrays, Strings


Deliverables

One source file: foodbank.c, for your solution to the given problem.

All files are to be submitted over WebCourses2/Canvas. (Do NOT submit .cpp files!!!)


Restrictions

Although you may use other compilers, your program must compile and run using Dev C++. Please use Dev C++ to develop your program. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. Also, make sure you include comments throughout your code describing the major steps in solving the problem.


Grading Details

Your program will be graded upon the following criteria:
1. Your correctness.
2. Your programming style and use of white space. Even if you have a plan and your program works perfectly, if your programming style is poor or your use of white space is poor, you could get 5% deducted from your grade.
3. Whether or not you have designed reasonable functions to solve the task at hand.
4. Compatibility with Dev C++ (in Windows). If your program does not compile in this environment, you will get a sizable deduction from your grade.




Here is my code:
// Charles Matusevich
// COP3223
// Secion 2
// foodbank.c
// 02/28/14

#include <stdio.h>
#include <string.h>

int main(void) {

    int choice, i, donc=0, reqc=0, stop=0;
    char dontype[100][20];
    int donamt[100];
    char reqtype[100][20];
    int reqamt[100];
    char found, where;

    printf("Welcome to the food Bank Program\n\n");
    printf("\t1. Add a donation\n");
    printf("\t2. Add a request\n");
    printf("\t3. Fulfill a request\n");
    printf("\t4. Print status report\n");
    printf("\t5. Exit\n\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    while (choice != 5) {

        if (choice == 1) {

            for (i=donc;i<donc+1;i++) {

                while (stop !=1) {

                    printf("\n\nEnter inventory type: ");
                    scanf("%s", &dontype[i]);
                    printf("Enter the amount: ");
                    scanf("%d", &donamt[i]);
                    printf("\nDonation Added!\n\n");
                    stop = 1;
                }
            }

            donc++;
            stop = 0;

        }

        else if (choice == 2) {

            printf("\n\t--- Add a request ---");

            for (i=reqc;i<reqc+1;i++) {

                printf("\n\nEnter inventory type: ");
                scanf("%s", &reqtype[i]);
                printf("Enter the amount: ");
                scanf("%d", &reqamt[i]);
                printf("\nRequest Added!\n\n");

            }

            reqc++;

        }

        else if (choice == 3) {

            printf("\n\n\tFulfilling Requests");

            if (reqc == 0)
                printf("\n\n\tThere are no requests to be fulfilled.\n\n\n");

            else if (donc == 0)
                printf("\n\n\tThere are no donations of that type.\n\n\n");

            else {

                found = 0;

                for (i=0;i<donc;i++) {

                    if (strcmp(dontype[i], reqtype[0])==0) {

                        found = 1;
                        where = i;
                    }
                }

                if (found == 0) {

                }

                if (reqamt[0] < donamt[where]) {

                    donamt[where] -= reqamt[0];

                for(i=0,i<reqc-1;i++) {

                        if (donc == 0) {

                            strcpy(reqtype[i], reqtype[i+1]);
                            strcpy(reqamt[i], reqamt[i+1]);

                        }
                }

                reqc--;

                }
        }

        else if (choice == 4) {

            printf("\n\nPrinting the Donations Table\n\n");

            for (i=0;i<donc;i++) {

                printf("\t\t%s  ", dontype[i]);
                printf("%d", donamt[i]);
                printf("\n");

            }

            printf("\n\nPrinting the Requests Table\n\n");

            for (i=0;i<reqc;i++) {

                printf("\t\t%s  ", reqtype[i]);
                printf("%d", reqamt[i]);
                printf("\n");

            }

            printf("\n\n");

        }

         printf("Welcome to the food Bank Program\n\n");
         printf("\t1. Add a donation\n");
         printf("\t2. Add a request\n");
         printf("\t3. Fulfill a request\n");
         printf("\t4. Print status report\n");
         printf("\t5. Exit\n\n");
         printf("Enter your choice: ");
         scanf("%d", &choice);

    }

    printf("\nThank You for using the software. Bye for now.\n\n");
    printf("Press any key to continue\n\n");
    system("pause");
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with food bank c programming assignment

#2 AKMafia001   User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: Help with food bank c programming assignment

Posted 07 March 2014 - 11:31 AM

Sorry! But, saying that my program won't run is not enough! And making us read all that text isn't fair too!

Be more specific... Why your program won't run? What problems are you having?

Also, post the errors that you are getting...
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Help with food bank c programming assignment

Posted 07 March 2014 - 12:13 PM

foodbank.c:38:33: warning: format specifies type 'char *' but the argument has
      type 'char (*)[20]' [-Wformat]
                    scanf("%s", &dontype[i]);
                           ~~   ^~~~~~~~~~~
foodbank.c:58:29: warning: format specifies type 'char *' but the argument has
      type 'char (*)[20]' [-Wformat]
                scanf("%s", &reqtype[i]);
                       ~~   ^~~~~~~~~~~
foodbank.c:96:39: warning: array subscript is of type 'char' [-Wchar-subscripts]
                if (reqamt[0] < donamt[where]) {
                                      ^~~~~~
foodbank.c:98:27: warning: array subscript is of type 'char' [-Wchar-subscripts]
                    donamt[where] -= reqamt[0];
                          ^~~~~~
foodbank.c:100:37: error: expected ';' in 'for' statement specifier
                for(i=0,i<reqc-1;i++) {
                                    ^
foodbank.c:105:36: warning: incompatible integer to pointer conversion passing
      'int' to parameter of type 'const void *' [-Wint-conversion]
                            strcpy(reqamt[i], reqamt[i+1]);
                                   ^~~~~~~~~
/usr/include/secure/_string.h:83:53: note: expanded from macro 'strcpy'
  __builtin___strcpy_chk (dest, src, __darwin_obsz (dest))
                                                    ^
/usr/include/secure/_common.h:39:54: note: expanded from macro '__darwin_obsz'
#define __darwin_obsz(object) __builtin_object_size (object, _USE_FORTIF...
                                                     ^
foodbank.c:105:36: warning: incompatible integer to pointer conversion passing
      'int' to parameter of type 'char *' [-Wint-conversion]
                            strcpy(reqamt[i], reqamt[i+1]);
                                   ^~~~~~~~~
/usr/include/secure/_string.h:83:27: note: expanded from macro 'strcpy'
  __builtin___strcpy_chk (dest, src, __darwin_obsz (dest))
                          ^
foodbank.c:105:47: warning: incompatible integer to pointer conversion passing
      'int' to parameter of type 'const char *' [-Wint-conversion]
                            strcpy(reqamt[i], reqamt[i+1]);
                                              ^~~~~~~~~~~
/usr/include/secure/_string.h:83:33: note: expanded from macro 'strcpy'
  __builtin___strcpy_chk (dest, src, __darwin_obsz (dest))
                                ^
foodbank.c:100:26: warning: expression result unused [-Wunused-value]
                for(i=0,i<reqc-1;i++) {
                        ~^~~~~~~
foodbank.c:115:9: error: expected expression
        else if (choice == 4) {
        ^
foodbank.c:154:5: warning: implicit declaration of function 'system' is invalid
      in C99 [-Wimplicit-function-declaration]
    system("pause");
    ^
foodbank.c:158:1: error: expected '}'
^
foodbank.c:11:16: note: to match this '{'
int main(void) {
               ^
9 warnings and 3 errors generated.


There are the errors and warnings.

You need to check your { } for starters.

Note, when using scanf with a string, a & is not needed.
Was This Post Helpful? 0
  • +
  • -

#4 damatuse   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 14

Re: Help with food bank c programming assignment

Posted 07 March 2014 - 04:21 PM

Not sure why the rest of my post didn't post but I put my question at the end.
The problem with my program is that my donations and requests don't add up. Like if I donate MILK 5 twice it will print it twice instead of printing MILK 10 when I request to see the donations table
Was This Post Helpful? 0
  • +
  • -

#5 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Help with food bank c programming assignment

Posted 07 March 2014 - 06:18 PM

I recommend the use of functions.

Quote

Like if I donate MILK 5 twice it will print it twice instead of printing MILK 10 when I request to see the donations table


Well then go to the part of the code that deals with that and separate print MILK and print amount. And, figure a better way of holding the amount donated, and make sure it adds it up properly. And why are you using string copy strcpy() with an int array?

This post has been edited by infernorthor: 07 March 2014 - 06:19 PM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Help with food bank c programming assignment

Posted 07 March 2014 - 09:27 PM

Looks like KP24 has the same issue about the wrong output in his implementation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1