当前位置: 代码迷 >> C语言 >> [求助]请指点小菜一下。
  详细解决方案

[求助]请指点小菜一下。

热度:314   发布时间:2005-11-02 01:15:00.0
[求助]请指点小菜一下。
请高手指点,这个程序题是输入联络人名,电话,地址,然后全部输出来,
输入数据是分开几次输入,但输出是把几次输入的数据一起输出。
本人做了,但现实不到题所说的。高手请指点。
#include <stdio.h>
#include <string.h>
struct tele /*defines the tele*/
{
char name[20];
long int num;
char address[30];
}abc; /*declares a variable*/

main()
{ int choice=1;
{
printf("1 add new subscriber details\n");
printf("2 view directory\n");
printf("3 quit\n");
printf("\n\n Enter choice:");
scanf ("%d",&choice);

if (choice==1)
addsubs();
else if(choice==2)
viewdir();
else if(choice==3)
exit();

}
}

addsubs()
{
FILE *fp;
struct tele abc;
int n;
char flage='y';
if ((fp=fopen("telephone","a"))==NULL)
{printf("\nerror open tele file");
}
while(flage=='y')
{printf(" enter cust name:");
scanf ("%s",&abc.name);
printf(" Enter cust No.:");
scanf("%d",&n);
if (2000000>n||n>2999999)
{printf ("sub tele no. must be between 2000000 and 2999999");
printf(" Enter cust No.:");
scanf("%d",&n);
}
abc.num=n;
printf(" Enter address:");
scanf("%s",&abc.address);
getchar ();
fwrite(&abc,sizeof(struct tele),1,fp);
printf("\n\n continue?(Y/N)");
scanf("%c",&flage);
}

fclose(fp);
}

viewdir()
{ FILE *fp;
char flage='y';
int i;
struct tele abc;
if((fp=fopen("telephone","r"))==NULL)
{ printf("打开文件出错\n");}
for (i=0;i<3;i++)
{fread(&abc,sizeof(struct tele),1,fp);
printf(" The customer name is %s\n",abc.name);
printf(" The customer no is %d\n",abc.num);
printf(" The customer address is %s\n",abc.address);
}
fclose(fp);

}

搜索更多相关的解决方案: 小菜  

----------------解决方案--------------------------------------------------------
哎,我的作业也是这个啊,选项=7啊,比你这个还复杂,我也再苦恼呢。

----------------解决方案--------------------------------------------------------
while(flage=='y')
{printf(" enter cust name:");
scanf ("%s",&abc.name);
printf(" Enter cust No.:");
scanf("%d",&n);
if (2000000>n||n>2999999)
{printf ("sub tele no. must be between 2000000 and 2999999");
printf(" Enter cust No.:");
scanf("%d",&n);
有错误!!!!!
----------------解决方案--------------------------------------------------------
fread(&abc,sizeof(struct tele),1,fp);
for (i=0;i<3;i++)
{
}
可以使用啊,我这里没问题。
我就修改了这个,还+了2个return(1).再你的函数最后。
----------------解决方案--------------------------------------------------------
  相关解决方案