2012-12-20 85 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