I'm creating a program using linked list which will later store in a text file. Basically this program will create an inventory, later update it as well. So I've successfully created the inventory (a function) and I've problem with updating it.
The inventory function is asking user to key in inputs, save into a text file and display it. The updating function has to select a particular item first and after that choosing whether to add or subtract the amount and the text file has to be updated at the same time.
So my problem is, I'm not able to compare the string input from the inventory function with this update function string input. Also, I need to receive an amount of item from user and then add the amount to the existing amount which I had entered in the inventory function earlier.
Visual Studio shows me that String 'code' might not be zero-terminated and String 'update' might not be zero-terminated for the update function.
I'm a beginner so I really need help from all of you, thank you!
This is the linked list codes:
struct donations
{
char supplyName[100], supplyCode[20], donator[150];
int no_ofShipment;
float quantityReceived;
struct donations* ptr;
} *start, *curr, *temp;
This is the inventory function (not a main function and will be changed later its a function for another main menu as the update function)
void main()
{
void details();
int i;
FILE *fp;
start = curr = NULL;
curr = start;
if (start == NULL)
{
start = curr = (struct donations *)malloc(sizeof(struct donations));
for (i = 0; i < 5; i++)
{
curr = (struct donations *)malloc(sizeof(struct donations));
details();
if (i == 0)
start = temp = curr;
else
{
temp->ptr = curr;
temp = curr;
}
}
temp->ptr = NULL;
temp = start;
printf("\n\t Inventory Created Successfully!");
printf("\n\t Recorded to the end of the list!");
}
//create file & print output into the file
if (fopen_s(&fp, "Donation.txt", "w") != 0)
{
printf("\nError");
return;
}
if (!fp)
{
printf("\n Error in opening file!");
//_getch();
return;
}
curr = start;
while (curr)
{
fprintf(fp, "%-30s", curr->supplyName);
fprintf(fp, "%-30s", curr->supplyCode);
fprintf(fp, "%-30s", curr->donator);
fprintf(fp, "%10d", curr->no_ofShipment);
fprintf(fp, "\t%.1f\n", curr->quantityReceived);
curr = curr->ptr;
}
printf("\n\n\t File (records) has been created!");
fclose(fp);
// display output
printf("\n\n\n\tDisplay Inventory");
while (temp)
{
printf("\n\n\n\t Name of Supply: %-30s", temp->supplyName);
printf("Supply Code: %-30s", temp->supplyCode);
printf("Donator: %-30s", temp->donator);
printf("No. of Shipment: %10d", temp->no_ofShipment);
printf("\tQuantity Received: %.1f", temp->quantityReceived);
temp = temp->ptr;
}
}
void details()
{
int i = 0;
system("cls");
printf("\tInventory Creation\t");
printf("\n\n\tEnter Name of Supply: ",(i+1));
gets_s(curr->supplyName);
while (getchar() != '\n');
printf("\n\tEnter Supply Code: ", (i + 1));
gets_s(curr->supplyCode);
while (getchar() != '\n');
printf("\n\tEnter Donator: ", (i + 1));
gets_s(curr->donator);
while (getchar() != '\n');
printf("\n\tEnter No. of Shipment: ", (i + 1));
scanf_s("%d", &curr->no_ofShipment);
while (getchar() != '\n');
printf("\n\tEnter Quantity Received: ", (i + 1));
scanf_s("%f", &curr->quantityReceived);
while (getchar() != '\n');
}
this is my update function, but not finished yet.
void update_DonationQuantity()
{
char code[10];
char update[5];
float quantity, Quantity;
FILE *fp;
printf("\n\tUpdate Donation Quantity\t\n");
printf("\n\n\tSelect donation (CT/HS/FM/SM/OM): ");
gets_s(code);
if (strcmp(code, curr->supplyCode) == 0)
{
printf("\n\tReceived/Distibuted item (+/-): ");
gets_s(update);
while (strcmp(update, "+") && (update, "-") != 0)
{
printf("\n\tWrong input, Try Again");
printf("\n\tReceived/Distibuted item (+/-): ");
gets_s(update);
}
if (strcmp(update, "+") == 0)
{
printf("\n\t Enter received amount: ");
scanf_s("%f", &quantity);
if (fopen_s(&fp, "Donation.txt", "a") != 0)
{
printf("\nError");
return;
}
if (!fp)
{
printf("\n Error in opening file!");
//_getch();
return;
}
curr->quantityReceived = quantity + curr->quantityReceived;
fprintf(fp, "\t%.1f\n", curr->quantityReceived);
}
}
}