I write this program to imitate a cash register and have the data for the stock currently in shop,the problem arise whenever i put the code = 10, when i use the function to read it, the program behave unexpectedly.the code = 10 is put at Update_storage function in itemlist.code variable while the reading part happen at read_storage function. I will attach the full code just in case the problem happen because of another function etc
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void Update_storage();
void read_storage();
void Create_File();
float send_price();
float checkout(float paid);
void display_receipt(float amountpaid,float baki);
int dataz;
int z;
int namez[100];
float pricez[100];
int quanz[100];
struct Storage{
int code;
char item_name[100];
float price;
int qty;
};
int main()
{
int choice, menu,qtyx,moreitem,comfirmation;
float y,a;
float sumprice = 0.0;
float totalprice = 0.0;
printf("Input any digit(from 1-10) to start the program
");
printf("!!! If you want to exit input 0 !!!
");
scanf("%d", &menu);
system("cls");
while(menu != 0)
{
printf("|***Welcome to Cash Register Prototype-0***|
");
printf("| Nice to be working with you!^_^ |
");
printf("|------------------------------------------|
");
printf("| Please choose what you would like to do |
");
printf("|(1)Update storage |
");
printf("|(2)Read storage |
");
printf("|(3)Create storage*!!!WARNING!!!* |
");
printf("|(4)Customer payment system |
");
printf("|------------------------------------------|
");
printf("*!!!WARNING!!!*USE ONLY FOR 1ST TIME RUNNING THIS PROGRAM
");
printf("-------->");
scanf("%d", &choice);
system("cls");
if(choice == 1){
Update_storage();
}else if (choice == 2){
read_storage();
}else if (choice == 3){
printf("
Choosing to proceed with this option will result in lost of data in storage if this is not your 1st use
");
printf("(1)Continue
(2)Return
----->");
scanf("%d", &comfirmation);
if(comfirmation == 1)
{
Create_File();
}
}else if (choice == 4){
moreitem = 1;
dataz = 0;
while(moreitem == 1)
{
y = send_price();
printf("Please enter the quantity of the item:");
scanf("%d", &qtyx);
quanz[dataz] = qtyx;
sumprice = y * qtyx;
pricez[dataz] = sumprice;
totalprice = totalprice + sumprice;
printf("Is there any other item in the cart: 1(YES) 2(NO)
----->");
scanf("%d", &moreitem);
if(moreitem == 1)
{
dataz++;
}
}
a = checkout(totalprice);
display_receipt(totalprice,a);
}else{
printf("Invalid Option");
}
totalprice = 0.0;
printf("
***Please Input (1)CONTINUE (0)EXIT***
");
scanf("%d", &menu);
system("cls");
}
printf("Hope to see you again in the future!!!!");
return 0;
}
void Create_File()
{
int i = 0;
struct Storage Item = {0," ",0.0,0};
FILE *pfcd = fopen("Item List.txt", "w+");
while(i < 100){
i++;
fwrite(&Item, sizeof(struct Storage),1, pfcd);
}
fclose(pfcd);
}
void Update_storage()
{
struct Storage itemlist;
FILE *Cftpr;
printf("Things to remember:
");
printf("(1)put this sign '_' instead of blank space for the item name(ex:Coca_Cola)
");
printf("(2)Input code = 0 to exit
");
printf("(3)Put the data arrangement as shown below");
printf("
Item_Name -> Price -> Quantity
");
if((Cftpr = fopen("Item List.txt", "r+"))== NULL)
printf("file cannot be open");
else{
printf("Code
");
scanf("%d", &itemlist.code);
while(itemlist.code != 0){
printf("
Item_Name -> Price -> Quantity
");
scanf("%s%f%d", &itemlist.item_name, &itemlist.price, &itemlist.qty);
fseek(Cftpr, (itemlist.code - 1)*sizeof(struct Storage),SEEK_SET);
fwrite(&itemlist, sizeof(struct Storage), 1, Cftpr);
printf("
Code
");
scanf("%d", &itemlist.code);
}
}
fclose(Cftpr);
}
void read_storage()
{
struct Storage itemlist;
int loop;
FILE *Cftpr;
if((Cftpr = fopen("Item List.txt", "r")) == NULL)
printf("File cant be open");
else{
printf("|-------------------------------------------|");
printf("
| *****ITEM INVENTORY***** |
");
printf("|-------------------------------------------|
");
printf("|%-6s%-16s%-11s%10s|
","code" ,"item_name" ,"price" ,"quantity" );
printf("|-------------------------------------------|
");
for(loop = 1 ; loop <= 100; loop++){
fread(&itemlist, sizeof(struct Storage), 1, Cftpr);
if(itemlist.code != 0)
printf("|%-6d%-16s%-11.2f%6d |
",itemlist.code ,itemlist.item_name ,itemlist.price ,itemlist.qty);
}
printf("|-------------------------------------------|
");
}
fclose(Cftpr);
}
float send_price()
{
int codex;
float pricex;
struct Storage itemlist;
FILE *senderboi;
printf("Enter the code:");
scanf("%d", &codex);
namez[dataz] = codex;
senderboi = fopen("Item List.txt", "r+");
fseek(senderboi, (codex - 1)*sizeof(struct Storage),SEEK_SET);
fread(&itemlist, sizeof(struct Storage), 1, senderboi);
printf("|-------------------------------------|
");
printf("|%-6s%-16s%11s |
", "code", "Product name", "Price");
printf("|-------------------------------------|
");
printf("|%-6d%-16s%11.2f |
",itemlist.code ,itemlist.item_name ,itemlist.price, itemlist.qty);
printf("|-------------------------------------|
");
fclose(senderboi);
pricex = itemlist.price;
return(pricex);
}
float checkout(float paid)
{
float taxincluded,change,amount;
taxincluded = paid * 1.06;
printf("
The total amount you need to pay is : %.2f
", taxincluded);
printf("/// SST is included in the shown price ///
");
printf("Enter the amount of money give by customer:");
scanf("%f", &amount);
change = amount - taxincluded;
printf("Amount Given : %.2f
", amount);
printf("Change : %.2f
", change);
return(change);
}
void display_receipt(float amountpaid,float baki)
{
float sstpaid;
float bayar;
FILE *receiptboii;
struct Storage Receipt;
amountpaid = amountpaid * 1.06;
sstpaid = 0.06 * (amountpaid / 1.060);
bayar = baki + amountpaid;
receiptboii = fopen("Item List.txt", "r+");
printf(" |---------------------------------------------|
");
printf(" |FLASH MART MALAYSIA BERKUMPULAN TAKDA HAD INK|
");
printf(" |Company No.: 1-388-2525 |
");
printf(" |SST No : P1Z00-1907-130082020 |
");
printf(" |tax receipt: s23ok-jebat |
");
printf(" |---------------------------------------------|
");
printf(" |%6s %16s%16s |
","qty", "Product", "Price");
printf(" |---------------------------------------------|
");
for(z = 0; z <= dataz;z++)
{
fseek(receiptboii, (namez[z] - 1)*sizeof(struct Storage),SEEK_SET);
fread(&Receipt, sizeof(struct Storage), 1, receiptboii);
printf(" |%4dx%17s%14s%-5.2f |
",quanz[z], Receipt.item_name, "RM", pricez[z]);
}
printf(" |---------------------------------------------|
");
printf(" | SST included :RM %-6.2f |
", sstpaid);
printf(" | Total Amount is :RM %-6.2f |
",amountpaid);
printf(" | Paid amount :RM %-6.2f |
",bayar);
printf(" | Change :RM %-6.2f |
",baki);
printf(" |---------------------------------------------|
");
printf(" | THANK YOU FOR SUPPORTING US |
");
printf(" | ALWAYS GIVE YOUR BEST TO TEACH US |
");
printf(" | ALTHOUGH WE NEVER SEE EACH OTHER FACE 2 FACE|
");
/*printf(" |stil reading this?what are you expect to see?|
");
printf(" | if you are still reading this i just want to|
");
printf(" | ask you why? there is is nothing left here |
");
printf(" | did you know there is 2 is there? |
");*/
printf(" |---------------------------------------------|
");
fclose(receiptboii);
return;
}
question from:
https://stackoverflow.com/questions/65925141/problem-with-either-reading-or-update-my-random-access-file