通过使用不同的结构code请用C项目经理 [英] Contact manager with c program by using structure different code

查看:149
本文介绍了通过使用不同的结构code请用C项目经理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有与另一个问题,之后我改变了我的代码,如下图所示,它仍然显示出了错误。编码里面,没有任何红色下划线,因此我无法找出的错误。所以这个编码的任何错误?

 结构接触
{
    炭名称[20],电子邮件[20];
    INT hpnum;
}加;
INT选项;
FILE * F;无效的addContact(结构的联系人列表[100]);
无效读取(结构的联系人列表[100]);INT主要(无效)
{
    结构联系人列表[100];
    系统(CLS);
    的printf(==========欢迎杰弗里的联系系统管理========== \\ n);
    的printf(\\ t \\ t \\ tContact系统主菜单\\ n);
    的printf([1]创建新的联系人\\ n);
    的printf([2]修改现有联系人\\ n);
    的printf([3]删除现有联系人\\ n);
    的printf([4]搜索现有联系人\\ n);
    的printf([5]退出\\ n);
    的printf(请输入你的选择之一\\ n);
    scanf函数(%d个,&安培;可选);
    开关(可选)
    {
        //添加新联系人
        案例1:的addContact(名单);读取(名单);
        打破;
    }    残培();
}无效的addContact(结构的联系人列表[100])
{
    炭名称[20],电子邮件[20];
    INT hpnum,无;    的printf(\\ n请名称:);
    scanf函数(%S,列表[无1]。名称);
    fflush(标准输入);
    的printf(\\ nHandphone号码:);
    scanf函数(%d个,&安培;列表[无1] .hpnum);
    fflush(标准输入);
    的printf(\\ NE-邮件:);
    scanf函数(%S,列表[无1] .email);
}无效读取(结构的联系人列表[100])
{
  FILE * F;
  F =的fopen(contact.txt,W);
  FWRITE(列表的sizeof(列表),100,F);
  FCLOSE(F);
}


解决方案

首先, fflush(标准输入); 是的undefined 错误。应该使用 fflush(标准输出);

二,函数的addContact(结构的联系人列表[100])变量的没有的没有分配任何价值你正在使用 scanf()的功能与垃圾的价值。

第三阅读()函数 FWRITE(列表的sizeof(列表),100,F); 错了,它应该是这样的。

  FWRITE(列表的sizeof(结构接触),100,F);

<子>我不知道,但它看起来你是为了阅读一个以上的接触所以你还需要内部的addContact可能是一个循环机制()函数

Now I having another problem with that, after I changed my coding as shown below, it still showing out the error. Inside the coding, there was no any red underline, therefore I can't find out where's the error. So any error of this coding?

struct contact
{
    char name[20],email[20];
    int hpnum;
}add;
int option;
FILE *f;

void addcontact(struct contact list[100]);
void read(struct contact list[100]);

int main (void)
{
    struct contact list[100];
    system("cls");
    printf("==========Welcome to Jeffery's Contact System Management==========\n");
    printf("\t\t\tContact System Main Menu\n");
    printf("[1] Create a New Contact\n");
    printf("[2] Modified Existing Contact\n");
    printf("[3] Delete Existing Contact\n");
    printf("[4] Search Existing Contact\n");
    printf("[5] Exit\n");
    printf("Please enter one of your option.\n");
    scanf("%d",&option);
    switch(option)
    {
        //add new contact
        case 1:addcontact(list);read(list);
        break;
    }

    getch();
}

void addcontact(struct contact list[100])
{
    char name[20],email[20];
    int hpnum,no;

    printf("\nContact Name: ");
    scanf("%s",list[no-1].name);
    fflush(stdin);
    printf("\nHandphone Number: ");
    scanf("%d",&list[no-1].hpnum);
    fflush(stdin);
    printf("\nE-mail: ");
    scanf("%s",list[no-1].email);
}

void read(struct contact list[100])
{
  FILE *f;
  f=fopen("contact.txt","w");
  fwrite(list,sizeof(list),100,f);
  fclose(f);
}

解决方案

First, fflush(stdin); is undefined error. should use fflush(stdout);

Second, in function addcontact(struct contact list[100]) variable no has not assigned any value and you are using in scanf() function with garbage value.

Third, in read() function fwrite(list,sizeof(list),100,f); is wrong it should be like.

fwrite(list, sizeof(struct contact), 100, f);

I am not sure but it looks You are intended to read more then one contacts So you also need a looping mechanism probably inside addcontact() function

这篇关于通过使用不同的结构code请用C项目经理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆