i have a problem with inputting data files and outputting them in the codes..
Here's the question: The format of the text file is:
(item code);(item name);(item price);(quantity of item in inventory)
The system allows the user to enter the item codes and quantity per item and then show the item’s price and the subtotal on the screen. Prompt an error message “Invalid item.” if the item code cannot be found in both text files. User will enter a sentinel value “-1” to indicate there is no item to be purchased. Lastly, prompt the receipt for customer on the screen.
I do not know how to use the file scanner in c language.
This is my code and i literally can only do for the first line.. which is code AG001
Code:
#include <string.h>
int main()
{
char itemCode[6];
char purchaseCode[6];
char itemName[30];
double price = 0, tprice = 0 ;
int stock, quantity, check, x, c, y;
FILE *fp;
FILE *fp2;
fp2 = fopen("ngst.txt", "r");
printf("\nEnter item codes: ");
scanf("%s", &itemCode);
printf("\nEnter quantity(-1 to exit): ");
scanf("%d", &quantity);
while(quantity != -1){
fp = fopen("gst.txt", "r");
fp2 = fopen("ngst.txt", "r");
do{
fscanf(fp,"%5s;%[^;];%lf;%d", purchaseCode, itemName, &price, &stock);
check = strcmp(itemCode, purchaseCode);
if (check == 0){
tprice = price * quantity;
printf("Item Code: %s\nItem Name: %s\nPrice:RM %.2lf\nTotal:RM %.2lf\n", purchaseCode,itemName, price, tprice);
x=0;
y=0;
}
else{
x = 0;
y =1;}
fclose(fp);
}while((c=fgetc(fp))!=EOF && x!=0);
do{
fscanf(fp2,"%5s;%[^;];%lf;%d", purchaseCode, itemName, &price, &stock);
check = strcmp(itemCode, purchaseCode);
if (check == 0){
tprice = price * quantity;
printf("Item Code: %s\nItem Name: %s\nPrice:RM %.2lf\nTotal:RM %.2lf\n", purchaseCode,itemName, price, tprice);
x=0;
y=0;
}
else{
if (y==1){printf("Invalid item.");}
x=0;}
fclose(fp2);
}while((c=fgetc(fp2))!=EOF && x!=0);
y=0;
printf("\nEnter item codes: ");
scanf("%s", &itemCode);
printf("\nEnter quantity(-1 to exit): ");
scanf("%d", &quantity);
x = 1;
}
system("pause");
}