2012-12-27 39 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