c语言制作通讯录发生的问题,求前辈指导,这个问题是我在自己买的c语言书籍学习中遇到的,是按照书上给的列题写到的,本来有些问题,改了之后发现还是有一些问题。。。。

intmainfunc1 2018-01-27 05:36:53


在制作通讯录的时候当输入删除指令的时候,会跳到menu函数里面出现下面的问题,比如说输入y之后
怎么解决之歌问题,让程序能够正常运行呢。
...全文
539 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2018-01-29
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LEN sizeof(struct addrRec)
#define QUIT 0  //定义通讯录

struct addrRec {
    char name[15];
    char tel[15];
    char workPlace[20];
    char email[30];
    struct addrRec *next ;
};

int menu(void);
void getInfo(struct addrRec *newRec);
struct addrRec *add(struct addrRec *);
void find(struct addrRec *);
struct addrRec *del(struct addrRec *);
void edit(struct addrRec *);
void showAll(struct addrRec *);
struct addrRec *insert(struct addrRec *head,struct addrRec *pNode);

int main()
{
    int choice;
    struct addrRec *head=NULL;
    while ((choice=menu())!=QUIT) {
        switch (choice) {
            case 1:
                showAll(head);
                break;
            case 2:head=del(head);
                   break;
            case 3:edit(head);
                   break;
            case 4:head=add(head);
                   break;
            case 5:find(head);
                   break;
            default:
                   printf("输入错误!请重试!\n");
        }
    }
    return 0;
}

//用户菜单
int menu()
{
    int code,status;
    printf("\n******请输入你的操作******\n");
    printf("1 显示所有联系人信息\n");
    printf("2 删除联系人\n");
    printf("3 修改联系人信息\n");
    printf("4 加入新的联系人\n");
    printf("5 查找联系人\n");
    printf("0 退出\n");

    while ((status=scanf("%d",&code))!=1||(code<0||code>5)) {
        while (getchar()!='\n')
            printf("请输入0到5之间的数。\n");
    }
    //fflush(stdin);
    return code;
}

void getInfo(struct addrRec *newRec)
{
    do{
        printf("\n请输入姓名(不能为空):");
        scanf("%s",newRec->name);
        printf("\n请输入联系电话(不能为空)\n");
        scanf("%s",newRec->tel);
    }while(strlen(newRec->name)==0||strlen(newRec->tel)==0);
    printf("\n请输入工作单位\n");
    scanf("%s",newRec->workPlace);
    printf("\n请输入电子邮件:\n");
    scanf ("%s",newRec->email);
}

//输出所有通讯记录
void showAll(struct addrRec *head)
{
    struct addrRec *p=head;
    if (!p) {
        printf("没有联系人。\n");
        return;
    }
    printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    printf("姓名  工作单位    联系电话    电子邮件");
    printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    while (p) {
        printf("%s\t\t%s\t\t%s\t\t%s\n",p->name,p->workPlace,p->tel,p->email);
        p = p->next;
    }
    printf("\n");
}
//查找指定姓名记录

struct addrRec *search(struct addrRec *head,char *name)
{
    struct addrRec *p = head;
    while (p && strcmp(p->name,name)!=0) {
        p = p->next;
    }
    return p;
}//输出联系热信息

void showRec(struct addrRec *pNode)
{
    printf("联系人%s的具体信息如下:\n",pNode->name);
    printf("工作单位[%s],电话[%s],电子邮件[%s].\n", pNode->workPlace,pNode->tel,pNode->email);
}

//查找指定联系人信息
void find(struct addrRec *head)
{
    char name[15];
    printf("\n请输入姓名:");
    //gets(name);
    scanf("%s", name);
    struct addrRec *p=search(head,name);
    if (!p)
        printf("联系人%s不存在。\n", name);
    else
        showRec(p);
}

//将指定结点插入升序列表:按姓名升序
struct addrRec *insert(struct addrRec *head,struct addrRec *pNode)
{
    struct addrRec *pPrev,*pNext;
    pNext=head;
    if (head==NULL)
    {
        head=pNode;
        pNode->next=NULL;
    }
    else{
        while ((pNext!=NULL)&&strcmp(pNode->name,pNext->name)>0)
        {
            pPrev=pNext;
            pNext=pNext->next;
        }
        if (pNext==NULL)
        {
            pPrev->next=pNode;
            pNode->next=NULL;
        } else
        {
            if (head==pNext)
            {
                head=pNode;
            } else
            {
                pPrev->next=pNode;
                pNode->next=pNext;
            }

        }
    }
    return head;
}
//增加联系人
struct addrRec *add(struct addrRec *head)
{
    struct addrRec *p=head;
    struct addrRec *newRec,*existed;
    char flag=0;
    newRec=(struct addrRec *)malloc(LEN);
    if (newRec==NULL) {
        printf("内存不足!无法创建用户。");
        exit(0);
    }
    getInfo(newRec);
    existed=search(p,newRec->name);
    if(existed!=NULL){
        puts("已有联系人:");
        showRec(existed);
        printf("是否修改?(Y/N)\n");
        scanf("%c",&flag);
        fflush(stdin);
        if(flag=='Y'||flag=='y'){
            strcpy(existed->tel,newRec->tel);
            strcpy(existed->workPlace,newRec->workPlace);
            strcpy(existed->email,newRec->email);
            printf("联系人%s的信息已跟新",newRec->name);
            free(newRec);
        }
    } else head=insert(head,newRec);
    printf("添加联系人成功!\n");
    return head;
}
//删除指定姓名的通讯录记录

struct addrRec *del(struct addrRec *head){
    struct addrRec *p;
    struct addrRec *pPre;
    char name[15];
    char flag;
    printf("请输入你要删除的联系人的姓名:");
    scanf("%s",name);
    p=head;
    while (p!=NULL&&strcmp(p->name,name)!=0) {
        pPre=p;
        p=p->next;
    }
    if (p==NULL) {
        printf("联系人%s不存在!请重试!",name);
    }
    else{
        showRec(p);
        printf("是否删除?(Y/N)");
        getchar();
        scanf("%c",&flag);
        //fflush(stdin);
        if (flag=='Y'||flag=='y'){
            if (p==head) {
                head=p->next;
            }
            else{
                pPre->next=p->next;
            }
            free (p);
        }
    }
    return head;
}
//对链表中的信息进行更改
void edit(struct addrRec *head){
    struct addrRec *p;
    char  flag;
    char name[15];
    printf("请输入你要修改的联系人的姓名:");
    //gets(name);
    scanf("%s", name);
    p=search(head,name);
    if (!p) {
        printf("联系人%s不存在!",name);
        return;
    }//修改存在的联系人
    printf("联系人%s已找到!\n",name);
    showRec(p);
    printf("是否修改?(Y/N)");
    getchar();
    scanf("%c",&flag);
    if (flag=='Y'||flag=='y') {
        getInfo(p);
    }
}
参考一下吧 主要的问题: scanf("%c", xx);和gets();这两个接收输入需要将之前输入残留在输入缓冲区里的'\n'换行符去掉,但是fflush(stdin);不能实现这个操作,我的解决办法是scanf("%c", xx);之前用getchar();接收换行符,在gets前也可以加上这句getchar():但是也可以将gets();换成scanf("%s", xx);解决。 对比一下代码看一下吧,代码里有一处问题,printf()缺少参数,也一并修改了。
intmainfunc1 2018-01-27
  • 打赏
  • 举报
回复
#include <stdio.h> #include "Header.h" int main() { int choice; struct addrRec *head=NULL; while ((choice=menu())!=QUIT) { switch (choice) { case 1: showAll(head); break; case 2:head=del(head); break; case 3:edit(head); break; case 4:head=add(head); break; case 5:find(head); break; default: printf("输入错误!请重试!\n"); } } return 0;} //用户菜单 int menu(){ int code,status; printf("\n******请输入你的操作******\n"); printf("1 显示所有联系人信息\n"); printf("2 删除联系人\n"); printf("3 修改联系人信息\n"); printf("4 加入新的联系人\n"); printf("5 查找联系人\n"); printf("0 退出\n"); while ((status=scanf("%d",&code))!=1||(code<0||code>5)) { while (getchar()!='\n') printf("请输入0到5之间的数。\n"); } fflush(stdin); return code; } void getInfo(struct addrRec *newRec){ do{ printf("\n请输入姓名(不能为空):"); scanf("%s",newRec->name); printf("\n请输入联系电话(不能为空)\n"); scanf("%s",newRec->tel); }while(strlen(newRec->name)==0||strlen(newRec->tel)==0); printf("\n请输入工作单位\n"); scanf("%s",newRec->workPlace); printf("\n请输入电子邮件:\n"); scanf ("%s",newRec->email); } //输出所有通讯记录 void showAll(struct addrRec *head){ struct addrRec *p=head; if (p==NULL) { printf("没有联系人。\n"); return; } printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); printf("姓名 工作单位 联系电话 电子邮件"); printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); while (p!=NULL) { printf("%s\t\t%s\t\t%s\t\t%s\n",p->name,p->workPlace,p->tel,p->email); p=p->next; } printf("\n"); } //查找指定姓名记录 struct addrRec *search(struct addrRec *head,char *name){ struct addrRec *p=head; while (p!=NULL&&strcmp(p->name,name)!=0) { p=p->next; } return p; }//输出联系热信息 void showRec(struct addrRec *pNode){ printf("联系人%s的具体信息如下:\n",pNode->name); printf("工作单位[%s],电话[%s],电子邮件[%s].\n",pNode->workPlace,pNode->tel,pNode->email); } //查找指定联系人信息 void find(struct addrRec *head){ char name[15]; printf("\n请输入姓名:"); gets(name); struct addrRec *p=search(head,name); if (p==NULL) { printf("联系人%s不存在。\n");} else showRec(p); } //将指定结点插入升序列表:按姓名升序 struct addrRec *insert(struct addrRec *head,struct addrRec *pNode) { struct addrRec *pPrev,*pNext; pNext=head; if (head==NULL) { head=pNode; pNode->next=NULL; } else{ while ((pNext!=NULL)&&strcmp(pNode->name,pNext->name)>0) { pPrev=pNext; pNext=pNext->next; } if (pNext==NULL) { pPrev->next=pNode; pNode->next=NULL; } else { if (head==pNext) { head=pNode; } else { pPrev->next=pNode; pNode->next=pNext; } } } return head; } //增加联系人 struct addrRec *add(struct addrRec *head){ struct addrRec *p=head; struct addrRec *newRec,*existed; char flag=0; newRec=(struct addrRec *)malloc(LEN); if (newRec==NULL) { printf("内存不足!无法创建用户。"); exit(0); } getInfo(newRec); existed=search(p,newRec->name); if(existed!=NULL){ puts("已有联系人:"); showRec(existed); printf("是否修改?(Y/N)\n"); scanf("%c",&flag); fflush(stdin); if(flag=='Y'||flag=='y'){ strcpy(existed->tel,newRec->tel); strcpy(existed->workPlace,newRec->workPlace); strcpy(existed->email,newRec->email); printf("联系人%s的信息已跟新",newRec->name); free(newRec); }}else head=insert(head,newRec); printf("添加联系人成功!\n"); return head; } //删除指定姓名的通讯录记录 struct addrRec *del(struct addrRec *head){ struct addrRec *p; struct addrRec *pPre; char name[15]; char flag; printf("请输入你要删除的联系人的姓名:"); scanf("%s",name); p=head; while (p!=NULL&&strcmp(p->name,name)!=0) { pPre=p; p=p->next; } if (p==NULL) { printf("联系人%s不存在!请重试!",name); } else{ showRec(p); printf("是否删除?(Y/N)"); scanf("%c",&flag); fflush(stdin); if (flag=='Y'||flag=='y'){ if (p==head) { head=p->next; } else{ pPre->next=p->next; } free (p); } } return head; } //对链表中的信息进行更改 void edit(struct addrRec *head){ struct addrRec *p; char flag; char name[15]; printf("请输入你要修改的联系人的姓名:"); gets(name); p=search(head,name); if (p==NULL) { printf("联系人%s不存在!",name); return; }//修改存在的联系人 printf("联系人%s已找到!\n",name); showRec(p); printf("是否修改?(Y/N)"); scanf("%c",&flag); fflush(stdin); if (flag=='Y'||flag=='y') { getInfo(p); } } 头文件是#ifndef Header_h #define Header_h #include <stdio.h> #include <stdlib.h> #include <string.h> #define LEN sizeof(struct addrRec) #define QUIT 0//定义通讯录 struct addrRec{ char name[15]; char tel[15]; char workPlace[20]; char email[30]; struct addrRec *next ; }; int menu(void); void getInfo(struct addrRec *newRec); struct addrRec *add(struct addrRec *); void find(struct addrRec *); struct addrRec *del(struct addrRec *); void edit(struct addrRec *); void showAll(struct addrRec *); struct addrRec *insert(struct addrRec *head,struct addrRec *pNode); #endif /* Header_h */
faihung 2018-01-27
  • 打赏
  • 举报
回复
你可以给出一个不是图片的代码吗? 不然帮你运行代码,还要一个一个手打。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧