2012-12-27 34 views
1

我正在执行一个订单的数据库,在这个数据库中处理一个事务。尽管我正在保存数据库,但数据库不在C上输出

的过程如下:

  1. 首先客户输入其ID。如果没有找到,它会退出到主菜单,否则它会继续(客户必须在数据库中注册一个ID,存储在另一个文件中)

  2. 它询问他们要购买哪个产品对于产品名称[这是演讲者想要的名称],如果没有发现它存在,否则它会出现

  3. 它问他们要购买多少,并检查是否有足够的股票。否则收益会询问用户是否要输入另一个号码或退出

  4. 价格自动计算并确认确认如果Cu然后stomer确认节省否则做了退出到主菜单

现在我的问题是,即使我的结构是节能,每当我来输出的任何订单数据库(现在的测试,因为我需要的客户的最后订单)数据库始终显示为EMPTY。下面是编码[这是相当长的我很抱歉,但我不明白哪里错了],也将提供列表全部功能。

截图提供,以及更好地了解程序是如何工作的:

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(); 

} 

以下是截图:

  1. http://tinypic.com/r/110x7c2/6
  2. http://tinypic.com/r/1446ya/6
  3. http://tinypic.com/r/315iy3s/6
  4. http://tinypic.com/r/15xo4lt/6
  5. http://tinypic.com/r/2ze9wfr/6
  6. http://tinypic.com/r/jtx8xw/6

我知道这是相当长的,但请原谅,我已经超过4小时,试图找出什么地方错了。谢谢一堆

+0

输出文件是否实际包含任何数据? – Woot4Moo

+0

是的,当我试图打开它时,出现了用户的名字和姓以及一堆垃圾NULL,但是数据是存在的。事实上,在完成交易后,它会输出所购买的商品等。并且全部从结构中取出,并且输出完美 – DodoSerebro

+1

为什么在使用变量'ofp'打开文件时,在输出函数中使用变量'pfp'?这些变量的声明在哪里,顺便说一句。 – Rubens

回答

1

您的oListAll()功能打开FILE *ofp,但从pfp读取。尝试阅读从ofp

+0

仍然没有,数据库仍然没有输出 – DodoSerebro

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 
    } 
} 
+0

我认为我的问题是它不是保存,我检查订单文件,它仍然只有8字节 – DodoSerebro

+0

请告诉我们定义了'order o'和'STRUCTSIZEO' – sb9

+0

order o是一个包含 名称,客户ID,价格,数量和time_t(用于时间戳)的结构的“副本” STRUCTSIZEO是sizeof(order) – DodoSerebro