Trouble moving lines up in 2D array
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;
}
I hope this is a little better...I added some comments too
Yeah I agree. It did that when I copied and pasted. Let me try re-posting it. I added a few comments so its easier to see what I'm trying to do. I know that there's easier ways to solving this problem but this is the way i need to do it to learn...
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;
//Ititialize quantity array for food request and donation to 0
for(l=0;l < 100; l++)
{
rquantity[l] = 0;
dquantity[l] = 0;
}
//Major loop that runs entire program
do
{ //Prompt user with menu choices
printf("Welcome to the Food Bank Program.\n\n");
printf("1. Add a donation\n2. Add a request\n3. Fulfill");
printf(" a request\n4. Print status report\n5. Exit\n\n");
printf("Please enter a choice from the above: ");
scanf("%d", &i);
//Check for valid input
while(i > 5 || i < 1)
{
printf("You have entered an incorrect choice, please enter a new choice.");
scanf("%d", &i);
}
//if statement that takes a donation
if(i == 1)
{ //Get temporary string name and quantity
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);
//Check to see if the string is already in the 2D array list
for(l=0; l<= j; l++)
{
//If the items are the same then add the quantities together
if(strcmp(temp, ditem[l]) == 0)
{
dquantity[l] += tempq;
k = 0;
}
}
//If item isnt found in 2D array then add to 2D array
if(k != 0)
{
strcpy(ditem[j], temp);
dquantity[j] = tempq;
j++;
}
printf("Donation added.\n");
}
//Choice 2-Request from the donated
if(i == 2)
{
//Get user input for the request
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);
//Look through request 2D array to update quantity requested
for(l=0; l<= m; l++)
{
if(strcmp(temp, ritem[l]) == 0)
{
rquantity[l] += tempq;
k = 0;
}
}
//If not already requested then add to request 2D array
if(k != 0)
{
strcpy(ritem[m], temp);
rquantity[m] = tempq;
m++;
}
printf("Request added.\n");
}
//Choice 3-Fulfill request
if(i == 3)
{
//for loop within for loop to compare all strings in both 2D arrays
for(z =0; z <= m; z++)
{
for(l = 0; l <= j; l++)
{
//if the strings are the same then fulfill the request
if(strcmp(ritem[z], ditem[l]) == 0)
{
dquantity[l] -= rquantity[z];
//if the donation quantity is 0 then "delete" string from both
//arrays and move all strings below up a position
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 there's more requests than donations: fulfill as many requests
//as possible, "delete" string from donation array
//(move all below string up a position),
//and update the request array.
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 less requests than donations: update donations
//and "delete" request string and move all strings below up
if(dquantity[l] > 0)
{
for(y = z; y <= m; y++)
{
strcpy(ritem[y], ritem[y+1]);
rquantity[y] = rquantity[y+1];
}
m--;
}
}
}
}
}
//Print a list of all donations and requests
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;
}