I am currently working on a food bank project but am running into a little trouble. I cant correctly delete a row from a 2D array and move all the rows below it up in position. I am trying this in the "if(i ==3)" statement...Any help will be greatly appreciated...thankyou!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main ()
{
char ditem[100][30], ritem[100][30];
char temp[30];
int dquantity[100], rquantity[100];
int i, j, k, l, m, z, y, tempq;
i = 0;
j = 0;
m = 0;
k = -1;
tempq = 0;
for(l=0;l < 100; l++)
{
rquantity[l] = 0;
dquantity[l] = 0;
}
do
{
printf("Welcome to the Food Bank Program.\n\n");
printf("1. Add a donation\n2. Add a request\n3. Fulfill a request\n4. Print status report\n5. Exit\n\n");
printf("Please enter a choice from the above: ");
scanf("%d", &i);
while(i > 5 || i < 1)
{
printf("You have entered an incorrect choice, please enter a new choice.");
scanf("%d", &i);
}
if(i == 1)
{
printf("Please enter the item name you would like to donate.\n");
scanf("%s", &temp);
printf("Please enter the quantity you would like to donate.");
scanf("%d", &tempq);
for(l=0; l<= j; l++)
{
if(strcmp(temp, ditem[l]) == 0)
{
dquantity[l] += tempq;
k = 0;
}
}
if(k != 0)
{
strcpy(ditem[j], temp);
dquantity[j] = tempq;
j++;
}
printf("Donation added.\n");
for(l=0; l < j; l++)
printf("%s %d\n", ditem[l], dquantity[l]);
}
if(i == 2)
{
printf("Please enter the item name you would like to request.\n");
scanf("%s", &temp);
printf("Please enter the quantity you would like to request.");
scanf("%d", &tempq);
for(l=0; l<= m; l++)
{
if(strcmp(temp, ritem[l]) == 0)
{
rquantity[l] += tempq;
k = 0;
}
}
if(k != 0)
{
strcpy(ritem[m], temp);
rquantity[m] = tempq;
m++;
}
printf("Request added.\n");
for(l = 0; l < m; l++)
printf("%s %d\n", ritem[l], rquantity[l]);
}
if(i == 3)
{
for(z =0; z <= m; z++)
{
for(l = 0; l <= j; l++)
{
if(strcmp(ritem[z], ditem[l]) == 0)
{
dquantity[l] -= rquantity[z];
if(dquantity[l] == 0)
{
for(y = l; y <= j; y++)
{
strcpy(ditem[y], ditem[y+1]);
dquantity[y] = dquantity[y+1];
}
j--;
for(y = z; y <= m; y++)
{
strcpy(ritem[y], ritem[y+1]);
rquantity[y] = rquantity[y+1];
}
m--;
}
if(dquantity[l] < 0)
{
rquantity[z] = abs(dquantity[l]);
for(y = l; y <= j; y++)
{
strcpy(ditem[y], ditem[y+1]);
dquantity[y] = dquantity[y+1];
}
j--;
}
if(dquantity[l] > 0)
{
for(y = z; y <= m; y++)
{
strcpy(ritem[y], ritem[y+1]);
rquantity[y] = rquantity[y+1];
}
m--;
}
}
}
}
}
if(i == 4)
{
printf("Here's a list of Donations:\n");
for(l = 0; l < j; l++)
printf("%s %d\n", ditem[l], dquantity[l]);
printf("\nHere's a list of Requests:\n");
for(l = 0; l < m; l++)
printf("%s %d\n", ritem[l], rquantity[l]);
}
} while(i != 5);
return 0;
}