我正在做一个订单数据库,在这种情况下处理交易。
程序如下:
首先,客户输入他的 ID。如果未找到,则退出到主菜单,否则继续(客户必须在数据库中注册一个 ID,该 ID 存储在另一个文件中)
它询问他们要购买哪种产品(它询问产品名称 [这是讲师想要的名称],如果没有找到它存在,否则它会继续
它询问他们要购买多少,并检查库存是否足够。如果是,则继续,否则询问用户是否要输入另一个数字或退出
自动计算价格并要求确认。如果客户确认,则保存完成,否则退出到主菜单
现在我的问题是,即使我的 STRUCT 正在保存,每当我在数据库中输出任何订单时(现在测试,因为我首先需要客户的最后一个订单)数据库总是显示为空。下面是编码[很长很抱歉,但我不明白哪里错了],并且还将提供 List All 功能。
还提供了屏幕截图以更好地了解该程序的工作原理:
void customerOrder()
{
int checkID = 0; //variable to hold the ID input
char ch;
char ch1;
char ch2;
char option;
char tempName [100];
int order = 0;
int tempStock = 0;
float tempPrice = 0;
printf ("\n\n\n\n\t\t ************* Add Customer Order *************\n \n \n");
// ----------- LOADING OF THE 3 DATA FILES -----------//
if ((ofp = fopen ("orders.dat","a+b")) == NULL)
{
fputs ("Error! Cannot open orders.dat\n",stderr);
system ("PAUSE");
orderMainMenu();
}
rewind (ofp);
if ((cfp = fopen ("customers.dat","r"))== NULL)
{
fputs ("Error! Cannot open customers.dat\n",stderr);
system ("PAUSE");
orderMainMenu();
}
rewind (cfp);
if ((pfp = fopen ("products.dat","r+b"))== NULL)
{
fputs ("Error! Cannot open products.dat\n",stderr);
system ("PAUSE");
orderMainMenu();
}
rewind (pfp);
//-------- Confirm whether to start Order ------------//
printf ("WARNING: In order for an Order to be made, the Customer must be in the Database\n");
printf ("Are you sure you want to continue? Y or N\n");
while (getchar() !='\n')
{
}
ch1 = getchar ();
if (ch1 == 'Y' || ch1 == 'y')
{
// ---- INPUT OF CUSTOMER ID --------------//
printf ("\nPlease Enter ID: ");
while (scanf ("%d",&checkID) == 0)
{
printf ("\n\nInvalid Input!!!\n");
printf ("Either you have entered a Letter!!\n");
printf ("Press 'Y' to enter another ID or any key to return to MainMenu\n\n");
while (getchar()!='\n')
{
}
option = getchar();
if (option == 'Y' || option == 'y')
{
printf ("\nPlease Enter Another ID Number:\n");
}
else
{
printf ("\nReturning to Order Management Menu\n");
system ("PAUSE");
fflush(stdin);
orderMainMenu();
}
}
//---------- CHECK WHETHER ID EXISTS OTHERWISE EXIT TO MENU --------------//
while (fread (&c, STRUCTSIZEC,1,cfp) == 1)
{
if (c.ID == checkID)
{
clrscr();
printf ("\n\n\n\n\t\t ************* Add Customer Order *************\n \n \n");
// SHOWS WHICH ID IS BEING SERVED //
printf ("\n\nNew Order For ID: %d\n", c.ID);
// ASKS WHICH PRODUCT TO BUY //
printf ("\nWhich Product do you want to buy?\n\n");
printf ("WARNING! Product Name is CASE SENSITIVE:\n");
// INPUT NAME //
printf ("Product Name: ");
while (getchar() !='\n')
{
}
fgets (tempName, 100, stdin);
while (fread (&p, STRUCTSIZEP,1,pfp)== 1)
{
if (strncmp (tempName,p.pName,sizeof(tempName)) == 0)
{
// --- SHOWING ID and WHICH PRODUCT CUSTOMER IS GOING TO BUY -- //
clrscr ();
printf ("\n\n\n\n\t\t ************* Add Customer Order *************\n \n \n");
printf ("Order for ID: %d\n", c.ID);
printf ("Product Name: %s\n\n", p.pName);
tempStock = p.pStock;
printf ("How many do you wish to buy?\n");
printf ("Currently there is %d in Stock", tempStock);
printf ("Order: ");
while (scanf ("%d",&order) == 0)
{
printf ("Invalid Order! Only Numbers are allowed!\n");
while (getchar() !='\n')
{
}
}
//---- CHECK WEHTHER ORDER IS BIGGER THAN WHAT IS FOUND IN STOCK ----//
//---- IF YES ASK IF USER WANTS TO INPUT ANOTHER NUMBER OR EXIT ----//
while (order > tempStock)
{
printf ("There is not enough items in Stock to satisfy that quantity!\n");
printf ("Do you want to enter another quantity? 'Y' for yes, any key to return to Menu\n");
fflush (stdin);
ch2 = getchar();
if (ch2 == 'Y' || ch2 == 'y')
{
printf ("Please enter another quantity:\n");
scanf ("%d",&order);
}
else
{
printf ("Order Canceled! Returning to Main Menu");
system ("PAUSE");
fclose (cfp);
fclose (ofp);
fclose (pfp);
orderMainMenu();
}
}
printf ("\nTotal Price for this Order will be:\n");
tempPrice = (order * p.pPrice);
printf ("Total: %.2f\n", tempPrice);
// ---- SHOW THE TRANSACTION OF THE USER AND ASK WHETHER TO CONFIRM ---- //
clrscr();
printf ("\n\n\n\n\t\t ************* Add Customer Order *************\n \n \n");
printf ("This is the Customer's Overview of Purchase:\n\n");
printf ("Customer's ID: %d\n",c.ID);
printf ("Customer's Product: %s",p.pName);
printf ("Order: %d\n",order);
printf ("Total Price: %.2f\n\n",tempPrice);
printf ("\n\n----------------------------------------\n\n");
printf ("Are you sure you of this transaction?\n");
printf ("Warning: After Confirming you cannot change the Order!\n");
printf ("Press 'Y' to confirm the Transaction otherwise press 'N' to cancel the order and return to Main Menu\n");
while (getchar() !='\n')
{
}
ch = getchar();
if (ch == 'N' || ch == 'n')
{
printf ("Transaction CANCELLED! Returning to Order Main Menu!\n");
system ("PAUSE");
orderMainMenu();
}
else if (ch == 'y' || ch == 'Y')
{
tempStock = (tempStock - order);
p.pStock = tempStock; //Updates the new stock number in Products' Database
fseek (pfp,-STRUCTSIZEP,SEEK_CUR);
fwrite(&p, STRUCTSIZEP,1,pfp);
fclose (pfp);
o.quantity = order;
o.cID = c.ID;
o.price = tempPrice;
strncpy(o.pName,p.pName, sizeof(p.pName));
o.timer = time(NULL);
fwrite (&o,STRUCTSIZEO,1,ofp);
fclose (ofp); //Closing of Files
fclose (cfp);
fclose (pfp);
printf("The Transaction Order saved is as follows:\n");
printf("ID: %d\nProduct: %sQuantity: %d\nPrice: %.2f\n",o.cID,o.pName,o.quantity,o.price);
printf("Transaction Made at: %s\n",asctime(localtime(&o.timer)));
system ("PAUSE");
orderMainMenu();
}
}
}
}
}
}
else
{
printf ("Returning to Order Main Menu\n");
system ("PAUSE");
orderMainMenu();
}
}
ListAll 方法:
void oListAll()
{
order o;
printf ("\n\n\n\n\t\t ********** Current Products in the Database *******\n \n \n");
//--------------- LOADING OF FILE ------------ //
if ((ofp = fopen ("orders.dat","rb")) == NULL)
{
fputs ("Cannot open products.dat file!\n",stderr);
printf ("Returning to Order Main Menu\n");
system ("PAUSE");
orderMainMenu();
}
rewind (ofp);
// --------- START TO TRAVERSE THE DATABASE AND OUTPUT DATA -------- //
printf ("Current Orders in the Database:\n");
while (fread (&o, STRUCTSIZEO,1,pfp)==1)
{
printf (" Name: %s Price: %.2f\n In Stock: %d\n\n", o.pName, o.price, o.quantity);
}
system ("PAUSE");
productMainMenu();
}
这些是屏幕截图:
- http://tinypic.com/r/110x7c2/6
- http://tinypic.com/r/1446ya/6
- http://tinypic.com/r/315iy3s/6
- http://tinypic.com/r/15xo4lt/6
- http://tinypic.com/r/2ze9wfr/6
- http://tinypic.com/r/jtx8xw/6
我知道这很长,但请耐心等待,我已经花了 4 个多小时试图弄清楚它有什么问题。谢谢一堆