Hello everyone,

I'm just getting started with C and I need help on this problem. It's a pretty long read so bare with me.

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 your program


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.

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.

When the user selects ‘4’, the program should print the 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.

This is what I have so far, which is pretty much nothing. Arrays and strings are KILLING ME!!!!

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

int main()
{
int choice, i, t;
char type[100][20];
int amount[100];
 
printf("Welcome to the food Bank Program.\n\n");
printf("\t1. Add a donation\n\t2. Add a request\n");
printf("\t3. Fulfill a request\n\t4. Print status report\n\t5. Exit\n\n");
printf("Enter your choice: ");
scanf("%d", &choice);

while (choice != 5)
{
if (choice == 1)
{
printf("\n\nEnter inventory type: ");
scanf("");
printf("Enter the amount: ");
scanf("");
printf("\n\nDonation Added!\n\n");
}
else if (choice == 2)
{
printf("\n\nEnter inventory type: \n");
scanf("");
printf("Enter the amount: ");
scanf("");
printf("\n\nRequest Added!\n\n");
}
else if (choice == 3)
{
printf("\n\n--------Fulfilling Requests--------");
printf("\n\nCannot be Fulfilled.\n\n");
printf("\n\nPartially Fulfilled.\n\n");
printf("\n\nRequest Fulfilled!\n\n");
}
else if (choice == 4)
{
printf("\n\nPrinting the Donations Table\n\n");
printf("\n\nPrinting the Requests Table\n\n");
}
else if (choice !=1,2,3,4,5)
{
printf("Sorry, that was an invalid choice.\n\n");     
}
printf("Welcome to the food Bank Program.\n\n");
printf("\t1. Add a donation\n\t2. Add a request\n");
printf("\t3. Fulfill a request\n\t4. Print status report\n\t5. Exit\n\n");
printf("Enter your choice: ");
scanf("%d", &choice);
}
printf("\nThank You for using the software. Bye for now.\n\n");
system("pause");    
return 0;    
}

Thanks in advance if anyone can help me with this! :)

Recommended Answers

All 16 Replies

While there may not be much to it yet, it is a start. I would, however, recommend being more careful with indentation. I would also suggest changing the long series of if()s to a single switch() statement, as that would probably be clearer:

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

int main()
{
    int choice, i, t;
    char type[100][20];
    int amount[100];

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

    while (choice != 5)
    {
        switch (choice)
        {
            case 1:
                printf("\n\nEnter inventory type: ");
                scanf("");
                printf("Enter the amount: ");
                scanf("");
                printf("\n\nDonation Added!\n\n");
                break;
            case 2:
                printf("\n\nEnter inventory type: \n");
                scanf("");
                printf("Enter the amount: ");
                scanf("");
                printf("\n\nRequest Added!\n\n");
                break;
            case 3:
                printf("\n\n--------Fulfilling Requests--------");
                printf("\n\nCannot be Fulfilled.\n\n");
                printf("\n\nPartially Fulfilled.\n\n");
                printf("\n\nRequest Fulfilled!\n\n");
                break;
            case 4:
                printf("\n\nPrinting the Donations Table\n\n");
                printf("\n\nPrinting the Requests Table\n\n");
                break;
            default:
                printf("Sorry, that was an invalid choice.\n\n");
        }
        printf("Welcome to the food Bank Program.\n\n");
        printf("\t1. Add a donation\n\t2. Add a request\n");
        printf("\t3. Fulfill a request\n\t4. Print status report\n\t5. Exit\n\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
    }
    printf("\nThank You for using the software. Bye for now.\n\n");
    return 0;
}

More to come as I go over the problem statement, if I get a chance.

Ok thank you very much for you help!

But remember I need this to be as intro as possible. I have yet learn what a switch statement is, what break;, case, or default do... (We still are in for loops, while loops, and if/else statements) And my professor hasn't taught us how to indent properly, lets put it this way, he doesn't even care, as long as the program does what he wants it to do and it works.

Tonight I will update the OP Code with indentations to see if they are correct and with some of the code we learned in class today.

And my professor hasn't taught us how to indent properly, lets put it this way, he doesn't even care, as long as the program does what he wants it to do and it works.

If so, then your professor is an imbecile who doesn't know his own business. Readability is one of the most important factors in programming; a working but unreadable (and hence unmaintainable) program is of little real use in the long run. You have to write programs as if they are to be read and changed, not just compiled.

Sorry to be so harsh, but this is an issue that I take very seriously, and I think you'll find that most others here do as well. If nothing else, there are enough programs such as astyle which can automatically indent the code for you that not indenting it is absolutely inexcusable for anyone but novices - it should have been one of the first things taught to you, and not doing so is a grave failure IMAO. I can understand that there are different styles of indentation, but indentation of some sort is nearly universal in C programming.

I'm not faulting you - you didn't know - but your professor's attitude is a major problem.

I noticed some issues with my earlier code, but since you haven't covered switch() statements in class yet, I'll hold off on posting the corrected code.

Ok so I added indentations into the code. I completely understand what you are saying and I totally agree with everything you said about indentation. His attitude is very annoying and he teaches programming so brokenly that I'm just lost. Programming at first was very simple and I was getting the hang of it, then the prof just took the course into a MAJOR jump that has surprised everyone in the class as well...

This is what I learned in class Monday on how to do part of step 3 which is fulfilling a request. This code could be all over the place, but I have no clue what in the world is going on. Kinda wish my class was more than just 50min long... I'm so lost :(

/*reqc = request count
donc = donation count
type = donation type (ie: milk, eggs, etc.)
reqtype = request type
donamt = donation amount
reqamt = request amount
found = ?
where = ?*/

if (reqc ==0)
else if (donc == 0)
else
    {
    found = 0;
    for (i=0;i<donc;i++)
        {
        if(strcmp(type[i], reqtype [0])==0)
            {
            found = 1;
            where = i;
            }
        }
    if (found == 0)
    else
    if (reqamt[0] < donamt[where])
        {
        donamt -= reqamt[0];
        for (i=0,i<(reqc-1);i++)
            {
            // Add strcpy code
            reqamt[i] = reqamt[i+1];
            }
        reqc--;
        }
    else if (reqamt[0] == donamt[where])
        {
        for (i=0,i<(reqc-1);i++)
            {
            // Add strcpy code
            reqamt[i] = reqamt[i+1];
            }
        reqc--;
        //remove donations
        for (i=where;i < donc-1;i++)
        }
    }

yea man I hate Lobo's class! can't believe someone else posted here about it. I'm working on the assignment right now as well, waiting to see what that retard has to say in class today..

edit: im also in the same spot as you

Oh -- you mean you two don't like that teacher because he makes you THINK? The assignment isn't really all that difficult if you code it in small steps. Sure learning is difficult, but isn't that the reason you are in school?

The new code posted has a very incomplete feel to it, as if it were from somewhere in the middle of some function. Could you please give some idea where this would be going in the program? I'm assuming that it would be somewhere after the menu, but where? It looks like the part for fulfilling a request, but I can't tell for certain.

By the way, has the professor gone over the subject of functions yet? I wouldn't expect so, unless the class is moving forward very rapidly, but it seems to be about where you might be starting to break the program into smaller parts.

Finally, as I'd asked before, if you tell us what parts are confusing you, we may be able to do more to help.

So this is what I have so far. Now I just gotta add part 3 which is the difficult part and the part I need help on. And yes that code that came from no where goes into part 3 of the main code. Also, check my indentation.

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

int main()
{
    int choice, i, donc=0, reqc=0, stop=0;
    char dontype[100][20];
    int donamt[100];
    char reqtype[100][20];
    int reqamt[100];
    
    printf("******* Welcome to the food Bank Program *******\n\n");
    printf("\t1. Add a donation\n\t2. Add a request\n");
    printf("\t3. Fulfill a request\n\t4. Print status report\n\t5. Exit\n\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    while (choice != 5)
    {
        if (choice == 1)
        {
            printf("\n\t--- Add a donation ---");
            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("\n\t* Donation 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("\n\t* Request Added! *\n\n");
            }
            reqc++;
        }
        else if (choice == 3)
        {
            printf("\n\n--------Fulfilling Requests--------");
            printf("\n\nCannot be Fulfilled.\n\n");
            printf("\n\nPartially Fulfilled.\n\n");
            printf("\n\nRequest Fulfilled!\n\n");
        }
        else if (choice == 4)
        {
            printf("\n\n\t--- Printing 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\n\t--- Printing 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");
        }
        else if (choice !=1,2,3,4,5)
        {
            printf("Sorry, that was an invalid choice.\n\n");     
        }
        printf("******* Welcome to the food Bank Program *******\n\n");
        printf("\t1. Add a donation\n\t2. Add a request\n");
        printf("\t3. Fulfill a request\n\t4. Print status report\n\t5. Exit\n\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
    }
    printf("\nThank You for using the software. Bye for now.\n\n");
    system("pause");    
    return 0;    
}

I am curious about the follow set of loops:

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("\n\t* Donation Added! *\n\n");
                    stop = 1;
                }
            }

With the for() loop, you have the value of i set to donc, the then comparison tests against donc+1; in other words, it stops after the first pass, as if the loop weren't there. Similarly, you have the while() loop testing against stop != 1, followed by unconditionally setting stop to 1 - again, ensure that there effectively isn't any looping. Why did you include these around them? This seems very cargo-cultish, as if you were copying a particular idiom you'd seen or used elsewhere (probably in the menu part, where you need the loop to verify that the input is valid) without really understanding it. The whole section could be boiled down to:

printf("\n\nEnter inventory type: ");
            scanf("%s", &dontype[i]);
            printf("Enter the amount: ");
            scanf("%d", &donamt[i]);
            printf("\n\t* Donation Added! *\n\n");
            donc++;

I hope you can see why this simplifies the code without losing any of its effect.

On another note, you have this conditional:

else if (choice !=1,2,3,4,5)
        {
            printf("Sorry, that was an invalid choice.\n\n");     
        }

Unfortunately, in C, you can't compare a single value against a list of values like this; the commas only confuse the issue, since they become comma operators and are effectively null operations. I'm not certain that this would even compile, to be honest. You really would need to check each one individually, or else check that they are in a range - though since it got this far, you really don't even need to do that, you could instead just use else (though you'd need a clause capturing the case of 5, which could, for example, do nothing).

else if (choice == 5)
        {}
        else
        {
            printf("Sorry, that was an invalid choice.\n\n");     
        }

I just noticed that I forgot to change the subscripts in one section - it should have been

printf("\n\nEnter inventory type: ");
            scanf("%s", &dontype[donc]);
            printf("Enter the amount: ");
            scanf("%d", &donamt[donc]);
            printf("\n\t* Donation Added! *\n\n");
            donc++;

However there is another problem with this code that I recommend attending to before tackling part three. As it is now, if two donations of the same type are processed - say, two entries of 'MILK' - then they are recorded separately. You need to read the donation type into a temporary buffer, then check through the list of donations to see if it is a match for an earlier one, and if it is, you need to add the amount of the new donation to the old one. The main function you'll need for this is strcmp(), string compare, which returns 0 if the two strings match, and strcpy(), to copy the value in the buffer to the dontype string when there's no match.

Since you'll need to use those two functions in the third part, it make sense to make sure you have them working correctly in the first part, which is less complicated.

AWESOME! Thank you for those tips. I got the strcmp()/strcpy() but I might have done too much then what was needed. Here is the changes I made for that.

printf("\n\t--- Add a donation ---");
            printf("\n\nEnter inventory type: ");
            scanf("%s", &dontype[donc]);
            printf("Enter the amount: ");
            scanf("%d", &donamt[donc]);
            printf("\n\t* Donation Added! *\n\n");
            donc++;
            if (strcmp(dontype[donc-2], dontype[donc-1]) == 0)
            {
                strcpy(dontype[donc-2], dontype[donc-1]);
                donamt[donc-2] += donamt[donc-1];
                donc--;
            }

Also edited the else if (choice == 5) to what you had, but the way I had it before did compile just fine. Just couldn't figure out how to exit the program if 5 was chosen, until I saw your simple logic.

On a side note, this thing is do Friday, so I will try my best to see if I can get Part 3 done before Thursday ends! Here goes nothing!

Ok so I'm trying to go step by step with Part 3 and I'm getting an error. I can't for the life of me figure it out!

else if (choice == 3)
        {
            printf("\n\n\t--------Fulfilling 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--;
                }
            }
        }

Never mind, found the errors in the part above, was missing semi colons in places. Man a good night sleep really helped clear up my mind :)

i'm struggling along with this assignment also, and i've already got the majority of mine typed out, but i tried to compile yours to see how it works, and i couldn't get it to work

any chance you could post your full code (what you have so far) so i could see how it works?

i am going to cry if i can't figure this out :(


edit: i fixed all the compile errors, and now i see there seems to be something wrong with your Part 3.

it seems to fulfill the request, and then delete the wrong entry? or something of this sort.

i'll keep checking it out.

in your choice 3 the two string copies seems to be going int to char

your program doesn't yet combine like food types, and the way it handles requests confuses me.

I'm still working on solving these issues myself, so i'll let you know if i figure anything out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.