我正在执行订单数据库,在这种情况下处理交易。
程序如下:
首先,客户输入他的ID。如果找不到,它会退出到主菜单,否则它会继续(客户必须在数据库中注册一个存储在另一个文件中的ID)
它询问他们将要购买哪种产品(它要求产品名称[这是讲师如何通过名称获得它],如果没有找到它,否则它会继续存在
它询问他们将购买多少并检查库存是否足够。如果是,则以其他方式询问用户是否要输入另一个号码或退出
价格自动计算并确认已确认。如果客户确认,则保存完成,否则退出到主菜单
现在我的问题是,即使我的STRUCT正在保存,每当我在数据库中输出任何ORDER时(现在进行测试,因为我需要客户的最后订单)数据库始终显示为EMPTY。下面是编码[相当长,我很抱歉,但我无法理解哪里出错]并且还将提供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();
}
以下是截图:
我知道它相当长,但请耐心等待我,我已经超过4个小时试图弄清楚它有什么问题。非常感谢
答案 0 :(得分:1)
您的oListAll()
功能会打开FILE *ofp
,但会从pfp
读取。尝试阅读ofp
答案 1 :(得分:1)
由于这不是整个程序,我找不到问题,但我猜它与你的文件指针是全局变量有关。您应该将它们设为本地,并始终确保它们正确关闭。
我会将customerOrder()函数分解为更小的函数。这将使您的代码更容易阅读,调试和修改。例如(这只是伪代码,你必须填写空白):
void customerOrder()
{
int checkId = getCustomerID(); // Checks the DB to see if user exists
bool productExists = checkProduct(tempName); // Checks the DB to see if product exists
int productCount = getProductCount(tempName); // Checks the DB to get count of items in stock
saveOrder(checkId, tempName, order); // Save the order
}
// Save the order in DB. Ensures FILE pointers are closed when done
void saveOrder(int customerID, const char * productName, int count)
{
order o;
// Create the order here....
FILE *ofp = fopen ("orders.dat","ab");
if (NULL != ofp) {
fwrite (&o,STRUCTSIZEO,1,ofp);
fclose (ofp); //Closing of Files
}
}