【问题标题】:the search function issue with contact system management联系人系统管理的搜索功能问题
【发布时间】:2013-03-02 07:22:33
【问题描述】:
int main (void){
    do
    {
        mainmenu:
        mainmenu();
        switch(option)
        {
                //add new contact
                case 1:addcontact(storage);savecontact(storage);
                    break;
                case 2:searchcontact();
            break;
        }
    }while(true);
}
void searchcontact()
{
    char searchname[20];
    system("cls");
    do
    {
        int find = 0;
        printf("Contact Search\n Name of the Contact:\n");
        fflush(stdin);
        scanf("%[^\n]",&searchname);
        length=strlen(searchname);
        f=fopen("contact.txt","rb");

        system("cls");
        printf("Search the result for %s\n",searchname);
        while(fread(&storage,sizeof(storage),space,f)==true)
        {
            for(i=0;i<=length;i++)
            storage[space].name[i]=storage[space].name[i];
            storage[space].name[length]='\0';
            if(stricmp(storage[space].name,searchname)==false)
                printf("Name\t:%s\nPhone\t:%d\nE-mail\t:%s\n",storage[space].name,storage[space].hpnum,storage[space].email);
                find++;
        }
        if(find==false)
            printf("\nNo match found!");
            else
            printf("\n %d match(s) found",find);
            fclose(f);
            printf("\nTry again?\t[1] Yes\t[2] No\n");
            scanf("%d",&choice);
    }while(choice==true);
}

我在搜索时遇到问题...添加联系人后,我无法再次搜索...我使用 stricmp 搜索联系人...同时我输入了我输入的正确名称以保存联系人,它仍然无法搜索出来...继续打电话给我再试一次。因此,该错误主要存在于那里的搜索功能上。我的真值为==1,我的假值为==0。

【问题讨论】:

  • 是C代码吗?缺少某些函数和变量定义,例如选项存储选择...您能提供缺少的部分吗?
  • @nullix 没关系...我正在尝试将我的编码上传到其他地方,你可以下载它...=)由于这里不能写太多编码,我宁愿你下载我的文件并了解我的整个编码,以便让您解决我的问题。 =) mediafire.com/?pkqsasr8forgjwv
  • 我将它移植到 linux 上进行测试,并修复了您的功能,请参阅答案。
  • 连接一次、创建个人资料然后再也不阅读是非常不友好的。回答人们花了几个小时阅读您的精彩代码。我回答了。

标签: string function search compare


【解决方案1】:

fread 返回多个已读项目(不是 true 或 false ...)。

  • 那么在您的情况下,仅当数据库中只有一项时它才返回 1。
  • searchname 是一个 char 数组,所以 searchname 是 char * (&searchname is char * *)

对不起代码,目前我无法正确地将其粘贴到 SO,它不想粘贴 c 缩进代码。我可以邮寄给你。

void searchcontact()
{
  char searchname[20],name[20];
  clearscreen();
  do {
      int i=0;
      int find = 0;
      int found = -1;
      int local_index=i%space;
      printf("Contact Search\n Name of the Contact:\n");
      fflush(stdin);
      //        scanf("%[^\n]",searchname);
      scanf("%s",searchname);
      length=strlen(searchname);
      f=fopen("contact.txt","rb");
      clearscreen();
      printf("Search the result for %s\n",searchname);
      while ( fread(&storage[local_index],sizeof(struct contact),1,f)==1 )
    {
      if(stricmp(storage[i%space].name,searchname)==false)
        {    
          printf("Name\t:%s\nPhone\t:%d\nE-mail\t:%s\n",storage[local_index].name,storage[local_index].hpnum,storage[local_index].email);
          find++;
          found=i;
        }
      i++;
      local_index=i%space;
    }
      if(find==false) printf("\nNo match found!");
      else printf("\n %d match(s) found",find);
      fclose(f);
      printf("\nTry again?\t[1] Yes\t[2] No\n");
      scanf("%d",&choice);
    } while(choice==true);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    相关资源
    最近更新 更多