当前位置: 代码迷 >> C语言 >> 求助一个问题 如何选择修改结构体中的数据
  详细解决方案

求助一个问题 如何选择修改结构体中的数据

热度:421   发布时间:2008-03-07 16:59:01.0
求助一个问题 如何选择修改结构体中的数据
void rewrite()
{
    int i;
    char id[10];
    printf("\n\t\t请输入要修改的学生学号:");
    scanf("%s",id);
    for(i=0;i<num;i++)
    {
         if(strcmp(student[i].id,id)==0)
         {
            printf("\n\t\t以下是您要修改的学生纪录:");
            printf("\n\t\t________________________________");
            printf("\n\t\t学号:  %s",student[i].id);
            printf("\n\t\t姓名:  %s",student[i].name);
            printf("\n\t\t电话:  %s",student[i].phone);
            printf("\n\t\t地址:  %s",student[i].adress);
            printf("\n\t\t邮编:  %s",student[i].postcode);
            printf("\n\t\te-mail:%s",student[i].e_mail);
            printf("\n\t\t________________________________");
            printf("\n\t\t请输入修改后的学生信息:");
            printf("\n\t\t输入姓名:");
            scanf("%s",&student[i].name);
            printf("\n\t\t输入电话号码:");
            scanf("%s",&student[i].phone);
            printf("\n\t\t输入地址:");
            scanf("%s",&student[i].adress);
            printf("\n\t\t输入邮编:");
            scanf("%s",&student[i].postcode);
            printf("\n\t\t输入e-mail:");
            scanf("%s",&student[i].e_mail);
            printf("\n\t\t是否继续修改?(Y/N):");
            if (getch()=='y')
                rewrite();
            return;
         }
    }
    printf("\n\t\t没有改学生的信息");
    printf("\n\t\t按任意键返回主菜单");
    getch();
    return;
}

这是我原来的代码,但是这样写就必须把所有的信息都改掉,如果我只选择其中一项修改应该怎么办??
谢谢
搜索更多相关的解决方案: 结构体  数据  选择  

----------------解决方案--------------------------------------------------------
加开关就好
比如说只想修改姓名
#ifdef   ONLYMODIFYNAME
            printf("\n\t\t输入姓名:");
            scanf("%s",&student[i].name);
#else
            printf("\n\t\t输入电话号码:");
            scanf("%s",&student[i].phone);
            printf("\n\t\t输入地址:");
            scanf("%s",&student[i].adress);
            printf("\n\t\t输入邮编:");
            scanf("%s",&student[i].postcode);
            printf("\n\t\t输入e-mail:");
            scanf("%s",&student[i].e_mail);
#endif
----------------解决方案--------------------------------------------------------
xiugai(struct stu *head)    /*修改数据*/
{
  int i=0;
  int n;
  char c;
  char num[20];
  struct stu *p=head,*r;
  clrscr();
while(1)
{
   clrscr();
   daying(head);
   printf("\t\t\tPlease input the num you want to amend:");
   scanf("%s",num);
   p=head;
   while(p->next!=NULL)
   {
    if((strcmp(p->xuehao,num))==0) break;
    p=p->next;
    i++;
    }
   if((strcmp(p->xuehao,num))!=0)
    {
      printf("\n\t\t\tThe number is not in!\n");
      continue;
     }
else
{
  while(1)
  {
    printf("\n\t\t\t0:Exit\n");
    printf("\n\t\t\t1:Amend Name\n");
    printf("\n\t\t\t2:Amend Number\n");
    printf("\n\t\t\t3:Amend Specialty\n");
    printf("\n\t\t\t4:Amend Grade\n");
    printf("\n\t\t\t5:Amend Class\n");
    printf("\n\t\t\t6:Amend Building\n");
    printf("\n\t\t\t7:Amend Floor\n");
    printf("\n\t\t\t8:Amend Dorm\n");
    printf("\n\t\t\t9:Amend Age\n");
    printf("\n\t\t\t10:Amend Sex\n");
    printf("\n\t\t\tPlease choise:");scanf("%d",&n);
    switch(n)
    {
      case 0:break;
      case 1:printf("\n\t\t\tName:");     scanf("%s",p->name);break;
      case 2:printf("\n\t\t\tNumber:");   scanf("%s",p->xuehao);break;
      case 3:printf("\n\t\t\tSpecialty:");scanf("%s",p->zhuanye);break;
      case 4:printf("\n\t\t\tGrade:");    scanf("%s",p->nianji); break;
      case 5:printf("\n\t\t\tClass");     scanf("%s",p->inclass);break;
      case 6:printf("\n\t\t\tBuilding:"); scanf("%s",p->louhao); break;
      case 7:printf("\n\t\t\tFloor:");    scanf("%s",p->lou);  break;
      case 8:printf("\n\t\t\tDorm:");     scanf("%s",p->sushe);break;
      case 9:printf("\n\t\t\tAge:");      scanf("%s",p->age); break;
      case 10:printf("\n\t\t\tSex:");      scanf("%s",p->sex); break;
      default:printf("\n\t\t\tError!\n");break;
     }
    printf("\n\t\t\tDo you go on to amend?(Y||N)\n");c=getch();
    if(c=='Y'||c=='y'){clrscr(); continue;}
    else break;
   }
  break;
  }
}
  
}
----------------解决方案--------------------------------------------------------
看不太明白,自己再研究下,谢谢帮助
----------------解决方案--------------------------------------------------------
这是我改的,大家帮我看下,为什么通过不了

void rewrite()
{
    int i;
    char id[10];
    char *name,*phone,*adress,*postcode,*e_mail;
    printf("\n\t\t请输入要修改的学生学号:");
    scanf("%s",id);
    for(i=0;i<num;i++)
    {
         if(strcmp(student[i].id,id)==0)
         {
            char *name,*phone,*adress,*postcode,*e_mail;
            printf("\n\t\t以下是您要修改的学生纪录:");
            printf("\n\t\t________________________________");
            printf("\n\t\t学号:  %s",student[i].id);
            printf("\n\t\t姓名:  %s",student[i].name);
            printf("\n\t\t电话:  %s",student[i].phone);
            printf("\n\t\t地址:  %s",student[i].adress);
            printf("\n\t\t邮编:  %s",student[i].postcode);
            printf("\n\t\te-mail:%s",student[i].e_mail);
            printf("\n\t\t________________________________");
            printf("\n\t\t请输入修改后的学生信息:");
            printf("\n\t\t输入姓名:");
            scanf("%s",name);
            if(name!='\0')
                strcpy(student[i].name=name);
            printf("\n\t\t输入电话号码:");
            scanf("%s",phone);
            if(phone!='\0')
                student[i].phone=phone;
            printf("\n\t\t输入地址:");
            scanf("%s",adress);
            if(adress!='\0')
                student[i].adress=adress;
            printf("\n\t\t输入邮编:");
            scanf("%s",postcode);
            if(postcode!='\0')
                student[i].postcode=postcode;
            printf("\n\t\t输入e-mail:");
            scanf("%s",e_mail);
            if(e_mail!='\0')
                student[i].e_mail=e_mail;
            printf("\n\t\t是否继续修改?(Y/N):");
            if (getch()=='y')
                rewrite();
            return;
         }
    }
    printf("\n\t\t没有该学生的信息");
    printf("\n\t\t按任意键返回主菜单");
    getch();
    return;
}
----------------解决方案--------------------------------------------------------
#include"stdio.h"
#include"string.h"
void rewrite()
{
    int i;
    char id[10];
    int num,mark1=0,mark2=1;
    printf("\n\t\t请输入要修改的学生学号:");
    scanf("%s",id);
    for(i=0;i<num;i++)
    {
         if(strcmp(student[i].id,id)==0)
         {  mark=1;
            printf("\n\t\t以下是您要修改的学生纪录:");
            printf("\n\t\t________________________________");
            printf("\n\t\t1.学号:  %s",student[i].id);
            printf("\n\t\t2.姓名:  %s",student[i].name);
            printf("\n\t\t3.电话:  %s",student[i].phone);
            printf("\n\t\t4.地址:  %s",student[i].adress);
            printf("\n\t\t5.邮编:  %s",student[i].postcode);
            printf("\n\t\t6.e-mail:%s",student[i].e_mail);
            printf("\n\t\t________________________________");
            printf("\n\t\t输入\t1\t修改姓名:");
            printf("\n\t\t输入\t2\t修改电话:");
            printf("\n\t\t输入\t3\t修改地址:");
            printf("\n\t\t输入\t4\t修改邮编:");
            printf("\n\t\t输入\t5\t修改e-mail:");
            printf("\n\t\t输入\t6\t修全部修改:");
            scanf("%d",&num);
            if(num==6)mark2=0;
            switch(num)
            {
            case 1:                
                    printf("\n\t\t输入姓名:");
                    scanf("%s",&student[i].name);
                if(mark2)
                {
                    break;
                }
            case 2:
                printf("\n\t\t输入电话号码:");
                scanf("%s",&student[i].phone);
            if(mark2)
                {
                    break;
                }
            case 3:
                printf("\n\t\t输入地址:");
                scanf("%s",&student[i].adress);
                if(mark2)
                {
                    break;
                }
            case 4:
                printf("\n\t\t输入邮编:");
                scanf("%s",&student[i].postcode);
            if(mark2)
                {
                    break;
                }
            case 5:
                printf("\n\t\t输入e-mail:");
                scanf("%s",&student[i].e_mail);
            if(mark2)
                {
                    break;
                }
                        
            }
        
    }

        if(mark==1)
            {
                printf("\n\t\t是否继续修改?(Y/N):");
                if (getch()=='y')
                rewrite();
                return;
            }
        else{
            printf("\n\t\t没有改学生的信息");
            printf("\n\t\t按任意键返回主菜单");
            getch();
            return;
        }
}
----------------解决方案--------------------------------------------------------
用switch写没问题可是用的时候显得太烦琐啊,我想可不可以判断输入的是不是enter,如果是则不保存,如果不是,则把输入的信息放到结构体数组中去,但是strcpy(student[i].name=name);这一句编译时worning,谁解释一下呀,谢谢
----------------解决方案--------------------------------------------------------
你得name是多大的数组啊?如果长度不够会出错
----------------解决方案--------------------------------------------------------
name[20],应该不是长度的问题吧,编译都通过不了
----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<string.h>
typedef struct
{
    char name[200];
}st;
void main()
{
    char name[200];
    st student;
    gets(name);
    strcpy(student.name,name);
    puts(student.name);
}
----------------解决方案--------------------------------------------------------
  相关解决方案