我的电话本成语死循环了。

shuirh 2008-12-13 02:03:25
#include<stdio.h>

struct pbook
{
char name[10];
char pnum[11];
struct pbook *next;
};

int n=0;

struct pbook *creat(void)
{
struct pbook *head=NULL,*p1,*p2;
p1=p2=(struct pbook *)malloc(sizeof(struct pbook));
printf("请输入姓名:");
scanf("%s",p1->name);
printf("请输入电话:");
scanf("%s",p1->pnum);
while(p1->name!='\0')
{
n=n+1;
if(n==1)
head=p1;
else
{
p2->next=p1;
p2=p1;
}
p1=(struct pbook *)malloc(sizeof(struct pbook));
printf("请输入姓名:");
scanf("%s",p1->name);
printf("请输入电话:");
scanf("%s",p1->pnum);
}
p2->next=NULL;
return(head);

}

void print(struct pbook *head)
{
struct pbook *p;
printf("电话薄中总共有%d条记录",n);
p=head;
while(p!=NULL)
{
printf("%s,%s\n",p->name,p->pnum);
p=p->next;
}
}

main()
{
struct pbook *p;
p=creat();
print(p);
}

试着做个最简单的电话本程序,但是死循环了,我怀疑是while(p1->name!='\0')出了问题,但是我不知道这里如何设置跳出循环条件比较好。还请大家多多指教,不胜感激。
...全文
122 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
waizqfor 2008-12-13
  • 打赏
  • 举报
回复
UP+mark
majun01 2008-12-13
  • 打赏
  • 举报
回复
对4楼做了一些修改,不用输入-1,直接点击 回车键Enter 就可以结束了。
#include <stdio.h>
#include <stdlib.h>

struct pbook
{
char name[10];
char pnum[11];
struct pbook *next;
};

int n=0;

struct pbook *creat(void)
{
struct pbook *head = NULL, *p1, *p2;
p1 = p2 = (struct pbook *)malloc(sizeof(struct pbook));
do
{

printf("请输入姓名:");

p1->name[0]=getchar(); //修改的部分
if ('\n'==p1->name[0]) //
{ //
free(p1); //
break; //
} //

scanf("%s", &p1->name[1]);
printf("请输入电话:");
scanf("%s", p1->pnum);
n = n + 1;
if (n == 1)
head = p1;
else
{
p2->next = p1;
p2 = p1;
}
p1 = (struct pbook *)malloc(sizeof(struct pbook));
getchar(); //修改的部分:添加了一句
}while(1);
p2->next = NULL;
return (head);
}

void print(struct pbook *head)
{
struct pbook *p;
printf("电话薄中总共有%d条记录\n",n);
p = head;
while (p != NULL)
{
printf("%s,%s\n", p->name, p->pnum);
p = p->next;
}
}

int main(void)
{
struct pbook *p;
p = creat();
print(p);

return 0;
}
shuirh 2008-12-13
  • 打赏
  • 举报
回复
4楼的程序做得非常好,只是用-1来跳出循环似乎有点不妥,没有人在输入名字的时候会输入-1吧!!!!
qq675927952 2008-12-13
  • 打赏
  • 举报
回复
up
lbh2001 2008-12-13
  • 打赏
  • 举报
回复
稍微修改一下,逻辑性更好,避免多于的输出和输入

#include <stdio.h>
#include <stdlib.h>

struct pbook
{
char name[10];
char pnum[11];
struct pbook *next;
};

int n=0;

struct pbook *creat(void)
{
struct pbook *head = NULL, *p1, *p2;
p1 = p2 = (struct pbook *)malloc(sizeof(struct pbook));
do
{
printf("请输入姓名:");
scanf("%s", p1->name);
if (!strcmp(p1->name, "-1")) break;
printf("请输入电话:");
scanf("%s", p1->pnum);
n = n + 1;
if (n == 1)
head = p1;
else
{
p2->next = p1;
p2 = p1;
}
p1 = (struct pbook *)malloc(sizeof(struct pbook));
}while(1);
p2->next = NULL;
return (head);
}

void print(struct pbook *head)
{
struct pbook *p;
printf("电话薄中总共有%d条记录\n",n);
p = head;
while (p != NULL)
{
printf("%s,%s\n", p->name, p->pnum);
p = p->next;
}
}

int main(void)
{
struct pbook *p;
p = creat();
print(p);

return 0;
}

goziem 2008-12-13
  • 打赏
  • 举报
回复
先顶一下 看看
kao331431214 2008-12-13
  • 打赏
  • 举报
回复
SF先
lbh2001 2008-12-13
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>

struct pbook
{
char name[10];
char pnum[11];
struct pbook *next;
};

int n=0;

struct pbook *creat(void)
{
struct pbook *head = NULL, *p1, *p2;
p1 = p2 = (struct pbook *)malloc(sizeof(struct pbook));
printf("请输入姓名:");
scanf("%s", p1->name);
printf("请输入电话:");
scanf("%s", p1->pnum);
while (strcmp(p1->name, "-1")) /*字符串要用strcmp函数比较,这里用名字为-1退出*/
{
n = n + 1;
if (n == 1)
head = p1;
else
{
p2->next = p1;
p2 = p1;
}
p1 = (struct pbook *)malloc(sizeof(struct pbook));
printf("请输入姓名:");
scanf("%s", p1->name);
printf("请输入电话:");
scanf("%s", p1->pnum);
}
p2->next = NULL;
return (head);
}

void print(struct pbook *head)
{
struct pbook *p;
printf("电话薄中总共有%d条记录\n",n);
p = head;
while (p != NULL)
{
printf("%s,%s\n", p->name, p->pnum);
p = p->next;
}
}

int main(void)
{
struct pbook *p;
p = creat();
print(p);

return 0;
}

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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