鱼C论坛

 找回密码
 立即注册
查看: 2595|回复: 6

help!

[复制链接]
发表于 2012-4-9 23:19:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#define MAX 20
struct data
{
  int phnum;
  int id;
  char name[30];
  char address[100];
};

struct data *sp,phlist[MAX];
  input(struct data *sp)
{int i;
for(i=0;i<MAX;i++)
  {  printf("input your phonenumber:");
scanf("%d",&phlist[i].phnum);
printf("input your id:");
scanf("%d",&phlist[i].id);
printf("input your name:");
scnaf("%c"&phlist[i].name);
printf("input your address:");
scanf("%c",&phlist[i].address);
  }
if(phlist[i].phnum==0)
   return(0);
else return(10);

}

void display(struct data *sp,int n)
{
  int i;
  printf("---------------------------------------");
  for(i=0;i<n;sp++,i++)
    printf("%d-10s%d-10s%s-10%s-20",sp->phnum,sp->id,sp->name,sp->address);
    }

void main()
{ int i;
  for(i=0;i<MAX;i++)
    {
      if(input(&phlist[i])==0)
    break;
      printf("\n");
    }
  display(phlist,i);
}


can't work out ? why?

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-4-10 00:51:59 | 显示全部楼层
I change " scanf()  " to "gets " . still no work out.???
:dizzy:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-4-10 12:33:25 | 显示全部楼层
本帖最后由 hxs554f 于 2012-4-10 22:15 编辑

#include <stdio.h>

#define MAX 3

struct data
{
  int phnum;
  int id;
  char name[30];
  char address[100];
};

input(struct data *sp)      //传进去的是指针,后面就不要用数组了
{                           //主函数里用了循环,这里就不必再用循环
        printf("input your phonenumber:");
        scanf("%d",&(sp->phnum));
        printf("input your id:");
        scanf("%d",&(sp->id));
        printf("input your name:");
        scanf("%s",sp->name);      //想读入字符串就用%s,且scanf拼写错误
        printf("input your address:");
        scanf("%s",sp->address);
        if(sp->phnum==0)
        return(0);
        else return(10);
        
}

void display(struct data *sp,int n) //主函数里用了循环,这里也不必再用循环了。当然,参数n也没用
{
//        int i;
        printf("---------------------------------------");
//        for(i=0;i<=n;sp++,i++)
                printf("%d-10s%d-10s%s-10%s-20\n",sp->phnum,sp->id,sp->name,sp->address);
//这句如果你是想两者之间空10个空格的话用printf("%10d-%10d-%10s-%20s",sp->phnum,sp->id,sp->name,sp->address);
}

void main()
{
        int i;
        struct data *sp,phlist[MAX];
        for(i=0;i<MAX;i++)
        {
                if(input(&phlist)==0)
                break;
                printf("\n");
                display(&phlist,i);  //这句应该放里面吧
        }
//        display(&phlist[0],i);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-4-10 23:21:59 | 显示全部楼层
Thanks so much for relay.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2012-4-13 00:35:10 | 显示全部楼层
你在最后调用display的时候穿的是结构体数组的首地址,而不是你要输出的那个结构体元素的地址
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-4-13 03:43:57 | 显示全部楼层

display(&phlist[0], i)  =  display(phlist,i)????
It will be the same?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-4-14 09:18:45 | 显示全部楼层
#include <stdio.h>
#define MAX 20
struct data
{
  int phnum;
  int id;
  char name[30];
  char address[100];
};
struct data phlist[MAX];

  input(struct data *sp)
{
printf("input your phonenumber:");
scanf("%d",&sp->phnum);
if(sp->phnum=='\0') return(0);

printf("input your id:");
scanf("%d",&sp->id);
printf("input your name:");
scanf("%s",&sp->name);
printf("input your address:");
scanf("%s",&sp->address);
  
  
  return(1);

  
}

void display(struct data *sp,int n)
{
  int i;
  printf("---------------------------------------\n");
  for(i=0;i<n;i++,sp++)
    printf("%-10d%-10d%-10s%-20s\n",sp->phnum,sp->id,sp->name,sp->address);
}

void main()
{
int c;  
for(c=0;c<MAX;c++)
    {
      if(input(&phlist[c])==0)
        break;
      printf("*****************\n");
    }
  
  display(phlist,c);
}


This is final result.   It take me a whole day. . Thanks again for everyone reply.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 21:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表