2012-12-20 45 views
-1

我在做一個搜索方法,其中檢查2個ID只存儲整數。搜索方法失敗

首先,我有一個客戶數據庫。我按順序輸入姓名,姓氏,身份證和地址,然後將這些文件立即保存到文件中

當用戶輸入身份證時,會調用此搜索方法並檢查所有文件以查看是否該ID是唯一的或不是。如果不是,那麼它返回0,否則返回1

現在的問題是這樣的。 當我輸入ID時,無論它是否是唯一的,它一直在繼續,但是當它輸出我寫的內容時,對於NAME和SURNAME,它只顯示我在那裏存儲的第一條記錄(如卡住某種的緩衝區),ID和地址輸出正常。

文件也沒有更新的意思,保存文件沒有發生。 現在當我刪除這個方法時,附加功能正常工作,但我無法訪問ID的比較。

任何建議爲什麼發生這種情況?如果可能的話,任何想法如何我可以解決它? 這就像每當我使用這種搜索方法時,整個文件從一開始就開始陷入困境。我試圖用布爾值使用該方法,但仍然無濟於事。當我嘗試使用它與布爾,而不是行「if(customerID(scanf(」%d「,& cCheck))== 1)」我做它== TRUE,它給我一個錯誤,輸出將 始終爲== FALSE,因爲數據不是NULL。

哦TRUE和FALSE是在我的情況下,有效的,因爲我有在COMMON.H

的代碼如下[張貼整個文件]一個typedef枚舉布爾: 涉及的方法[無效addCustomer ()]和[int customerID(int cCheck) 但我發佈全部,因爲其中一些互連。

編輯! - 即使它們不是唯一的ID,它仍然被接受...

/* 
* CustomerMainMenu.c 
* Author: DodoSerebro 
* 
* This class will output the Customer's Main Menu and re-directs to the 
* corresponding section 
* 
*/ 
#include<io.h> 
#include<fcntl.h> 
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <ctype.h> 
#include "..\Headers\common.h" 
#include "..\Headers\customerManagement.h" 

static FILE *cfp; 
static customer c; 
#define STRUCTSIZE sizeof (customer) 


/** This is the Customers's Main Menu in which the various sections can be 
* accessed from here 
*/ 
boolean customerMainMenu() 
{ 



    int optionC; 
    clrscr(); 

    copyright(); 


    printf ("\n\n\n\n\t\t ************* Customer's Main Menu *************\n \n \n"); 

    printf ("Press [1] to add a new Customer\n"); 
    printf ("Press [2] to edit a Customer\n"); 
    printf ("Press [3] to list all Customers\n"); 
    printf ("Press [4] to Show a Customer's last Order\n"); 
    printf ("Press [5] to go back to Main Menu\n\n\n"); 


    if (scanf ("%d",&optionC) == 1) 
    { 
     switch (optionC) 
     { 

     case 1: 
     { 
      clrscr(); 
      getchar(); 
      addCustomer(); 
      break; 
     } 
     case 2: 
     { 
      printf ("Edit a Customer\n"); 
      break; 
     } 

     case 3: 
     { 
      clrscr(); 
      listCustomers(); 
      system ("PAUSE"); 
      break; 
     } 
     case 4: 
     { 
      printf ("Customer's Last Order\n"); 
      break; 
     } 
     case 5: 
     { 
      system ("PAUSE"); 
      break; 
     } 
     default: 
     { 
      if (optionC != 1 || optionC != 2 || optionC != 3 || optionC != 4 || optionC !=5) 
      { 
       clrscr(); 
       printf ("Invalid option!\n"); 
       system ("PAUSE"); 
       customerMainMenu(); 
      } 
      break; 
     } 
     } 
    } 
    return TRUE; 

} 

/** 
* This following method will append a customer to the 
* database at the end of the file 
* 
* */ 

void addCustomer() 
{ 
    char ch; 
    copyright(); 

    printf ("\n\n\n\n\t\t ************* Add Client **********\n \n \n"); 

    if ((cfp = fopen ("customers.dat","a+b")) == NULL) 
    { 
     fputs("Can't open customers.dat file\n",stderr); 
    } 



    printf ("\tThis will add another customer to the the database\n"); 
    printf ("\tPress 'Y' to confirm or 'N' to return to the Client Main Menu\n\tWITHOUT adding a customer\n"); 
    ch = getchar(); 

    if (ch == 'n' || ch == 'N') 
    { 
     customerMainMenu(); 
    } 
    else if (ch == 'y' || ch == 'Y') 
    { 
     fflush(stdin); 
     clrscr(); 
     printf ("\n\n\n\n\t\t ************* Add Client **********\n \n \n"); 
     printf ("Please enter Name:\n"); 
     while (scanf ("%s", c.name) == 0 || cCheck(c.name,100) == FALSE); 
     { 

     } 


     printf ("Please Enter Surname: \n"); 
     while (scanf ("%s",c.surname) == 0 && cCheck (c.surname,100) == FALSE); 
     { 

     } 
     printf ("Please Enter ID Card, [NOTE! Only numbers are allowed!]\n"); 
     int cCheck; 
     if (customerID(scanf ("%d",&cCheck)) == 1) 
     { 
      printf ("ID already Taken, Client exists!\n"); 
      printf ("Do you want to enter another ID? 'Y' for Yes and 'N' to return to Main Menu\n"); 
      ch = getchar(); 
      if (ch == 'Y' || ch == 'y') 
      { 
       scanf ("%d",&cCheck); 
       customerID(cCheck); 
       c.ID = cCheck; 
      } 
      else 
      { 
       customerMainMenu(); 
      } 
     } 
     else 
     { 
      c.ID = cCheck; 
     } 


     getchar(); 

     printf ("Please Enter Address:\n"); 
     gets(c.address); 


     fwrite (&c,STRUCTSIZE, 1, cfp); 

     printf ("For Testing purposes:\n"); 
     printf (" %s\n %s\n %s\n %d\n", c.name, c.surname, c.address, c.ID); 
     askAnother(); 


    } 
    else 
    { 
     printf ("\nInvalid choice! Either Y or N is accepted\n"); 
     system ("PAUSE"); 
     getchar(); 
     clrscr(); 
     addCustomer(); 
    } 
} 

void listCustomers() 
{ 

    if ((cfp = fopen ("customers.dat","rb")) == NULL) 
    { 
     fputs("Can't open customers.dat file\n",stderr); 
     printf ("Returning to Customer Main Menu"); 
     system ("PAUSE"); 
     customerMainMenu(); 
    } 

    rewind (cfp); 
    while (fread (&c,STRUCTSIZE,1,cfp)==1) 
    { 
     printf ("Customer: %s %s ID: %d\n", c.surname, c.name, c.ID); 
    } 
    fclose (cfp); 


} 


void askAnother() 
{ 
    printf ("Do you want to add another Customer?\n"); 
    printf ("Enter 'Y' for yes and 'N' to return to the Main Menu\n"); 

    char input; 
    input = getchar(); 

    if (input == 'Y' || input == 'y') 
    { 
     getchar(); 
     addCustomer(); 
    } 
    else if (input == 'N'|| input == 'n') 
    { 

     fclose (cfp); 
     customerMainMenu(); 

    } 
    else 
    { 

     printf ("Invalid Option! Only Y or N are allowed\n"); 
     system ("PAUSE"); 
     clrscr(); 
     askAnother(); 

    } 

} 


boolean cCheck(char *test, int max) 
{ 
    int x; 
    for (x =0; x<max; x++) 
    { 
     if (isdigit(test[x])) 
     { 
      return FALSE; 
     } 
     if (x==max) 
     { 
      return TRUE; 
     } 
     x++; 

    } 
    return TRUE; 
} 

/** 
* This method will compare the ID passed from the ID of the customer to check 
* whether it is exists or not. If it exists it will output 1 otherwise it 
* will output -1. This will make sure that the Person's ID is unique 
* 
*/ 


int customerID (int cCheck) 
{ 

    rewind (cfp); 
    while (fread (&c,STRUCTSIZE,1,cfp)==1) 
    { 
     if (c.ID == cCheck) 
     { 
      return 1; 
     } 
     else 
     { 
      return 0; 
     } 
    } 

    return 1; 
} 

編輯!

上傳圖片展示了我的意思,如果我不清楚

(請注意如何姓名從這些輸入不同) HTTP://s017.radikal.ru/i443/1212/c8 /1ea9bc56d980.jpg

下面顯示我有什麼文件中 (只有一個文件) HTTP://s017.radikal.ru/i431/1212/49/2a0df6acf9ec.jpg

+2

現在給我一個失敗的行號,不要指望我通讀這個爛攤子。 – 2012-12-20 22:40:29

+0

145是它開始失敗.. 然後SearchMethod從273行開始 – DodoSerebro

回答

0
if (customerID(scanf ("%d",&cCheck)) == 1) 

在這裏,你是scanf()函數的返回值離子customerID()而您實際上想要通過掃描值cCheck

可以在呼叫分開運作,並輸入如下閱讀:

if(scanf ("%d",&cCheck) != 1) { 
    // Input error handling 
    } 
    else if (customerID(cCheck) == 1) { 
    ... 

的另一個問題是,你有一個名爲cCheck功能,你也有一個局部變量命名cCheck。適當地重命名其中一個。

+0

非常感謝,方法現在可以工作,但有一個小問題。在我添加記錄後,它不會附加到文件,這意味着保存文件被忽略 – DodoSerebro

+0

嘿所有,我設法修復它:)非常感謝您的幫助 – DodoSerebro