标题:程序一运行就弹出exe停止工作
只看楼主
听阳光洒下
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2013-11-10
结帖率:90.91%
已结贴  问题点数:20 回复次数:7 
程序一运行就弹出exe停止工作
#include<stdio.h>
#include<stdlib.h>
typedef struct student{
long no;
char name[20];
char sex;
int age;
}STUDENT;
#define MAXSIZE 100
STUDENT stu[MAXSIZE];
void create(STUDENT stu[],int n);
void output(STUDENT stu[],int n);
void search(STUDENT stu[],int n);
void insert(STUDENT stu[],int *pn);
void del(STUDENT stu[],int *pn);
int main()
{int n;
STUDENT stu[MAXSIZE];
int cord;
printf("\n请输入学生记录个数n=");
scanf("%d",&n);
create(stu,n);
while(1)
{printf("\n----------学生信息管理主菜单----------\n");
printf("\t1.显示学生信息\n");
printf("\t2.查找学生信息\n");
printf("\t3.插入学生信息\n");
printf("\t4.删除学生信息\n");
printf("\t5.退出\n\n");
printf("\t\t请选择(1~5):");
scanf("%d",&cord);
printf("\n-----------------------------------------\n");
switch(cord){
    case 1: output(stu,n);break;
    case 2: search(stu,n);break;
    case 3: insert(stu,&n);break;
    case 4: del(stu,&n);break;
    case 5: return 0;
    default:printf("\n输入有误,请重新输入!\n");}
}
 }
void create(STUDENT stu[],int n)
{int i;
for(i=0;i<n;i++)
{printf("\n学号:",stu[i].no);
scanf("%ld",&stu[i].no);
printf("姓名:",stu[i].name);
scanf("%s",stu[i].name);
printf("性别:",stu[i].sex);
scanf("%s",stu[i].sex);
printf("年龄:",stu[i].age);
scanf("%d",&stu[i].age);} }
void output(STUDENT stu[],int n)
{int i=1;
STUDENT *p;
printf("\n序号\t学号\t姓名\t性别\t年龄\n");
for(p=&stu[0];p<stu+n;p++)
{printf("%d\t",i++);
printf("%ld\t",p->no);
printf("%s\t",p->name);
printf("%s\t",p->sex);
printf("%d\t",p->age);
printf("\n");}
}
void search(STUDENT stu[],int n)
{int i;
long x;
printf("\n请输入待查找的学号");
scanf("%ld",&x);
for(i=0;i<n;i++)
if(stu[i].no==x) break;
if(i<n) printf("%ld\t%s\t%s\t%d\n",stu[i].no,stu[i].name,stu[i].sex,stu[i].age);
else printf("\n查找失败!" );
return;}
void insert(STUDENT stu[],int *pn)
{int i;
if(*pn==100)
{printf("顺序表已满,不能插入!");
return;}
else
  {i=*pn;
  printf("\n请依次输入");
  printf("\n学号  姓名  性别  年龄\n");
  scanf("%ld%s%s%d",&stu[i].no,stu[i].name,&stu[i].sex,&stu[i].age);
  *pn=*pn+1;}
  return;}
  void del(STUDENT stu[],int *pn)
  {int i;
  long n;
  if(*pn==0)
  {printf("此顺序表为空表,无法删除!");
  return;}
  else
   {printf("请输入要删除的学号:");
   scanf("%ld",&n);
   if(n=stu[i].no)
   {printf("删除成功!\n");
   *pn=*pn-1;}
   else
   printf("所输入学号不在该顺序表内,删除失败!\n");} }


这个程序一运行到那个年龄那里就说exe停止工作,不知道是为什么,求高手帮忙解答
搜索更多相关主题的帖子: include search create insert 
2013-11-10 14:02
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
得分:0 
你的sex定义为char,用%s接收,肯定会出错啊


[fly]存在即是合理[/fly]
2013-11-10 14:13
听阳光洒下
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2013-11-10
得分:0 
回复 2楼 azzbcc
万分感谢
2013-11-10 15:54
听阳光洒下
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2013-11-10
得分:0 
回复 2楼 azzbcc
#include<stdio.h>
      #include<malloc.h>
      #include<string.h>
      #include<stdlib.h>

       typedef struct{
        long no;
        char name[20];
        char sex[5];
        int age;
        }student;

        typedef struct node
         {student data;
          struct node *next;
         }linklist;
         linklist *input()          /*输入学生信息*/
     {
         linklist *p;
         p =(linklist *)malloc (sizeof (linklist ));
         printf("\n 请输入学号:");
         scanf("%ld",p->data.no);
         printf("\n 请输入姓名:");
        scanf("%s",p->data.name);
         printf("\n 请输入性别:");
         scanf("%s",&p->data.sex);
         printf("\n 请输入年龄:");
         scanf("%d",&p->data.age);
         return p;
      }
        void  displayAll(linklist *L)  /*显示学生信息*/
        {linklist *p;
        p=L->next;
        printf("输入学生的学号、姓名、性别、年龄\n");
        while(p!=NULL)
        {printf("\n%10d",p->data.no);
        printf("\t\t");
        printf("%s",p->data.name);
        printf("\t\t");
        printf("%s",p->data.sex);p=p->next;
        printf("\t\t");
        printf("%d\n",p->data.age);
        }
        }
         linklist *creatlist()  /*尾插法建表*/
    {       int i=0;
             linklist *s,*r,*p ;
             p=(linklist *)malloc(sizeof(linklist));
             r = p ;
        while(i==0)
        { s=input();
         r->next=s;
         r=s;
         printf("\n如果想继续输入,请输入0 \n");
         scanf("%d",&i);
         }
         r->next=NULL;
         displayAll(p);
         return p;
         }

      int listlength(linklist *head)  /*计算表长*/
       {
       int j;
       linklist *q;
        q=head->next;
        j=0;
        while(q!=NULL)
        {q=q->next;
        j++;
        }
        return j;
       }
       linklist  *locate( linklist  *head ,char name[20] ) /*以姓名查找*/
      {
          linklist  *s;
          s= head->next;
          while ( s!= NULL )
             if ( s->data.name!=name)
                 s = s->next;
            else
                 break ;
          return  s;
       }
       void insert (linklist *head, long no)/*在指定学号后插入学生信息*/
       {
           linklist  *q,*s,*r;
           q=head;
           while(q->data.no!=no)
            q=q->next;
            s =(linklist *)malloc (sizeof( linklist)) ;
            r=input();
            s->next=q->next;
            q->next=s;
       }
      
           linklist *dellist(linklist *head,long no)  /*删除指定学号信息*/
       {
           linklist  *p,*q;
           p = head ;
           while (( p->next != NULL)&& (p->data.no!=no))
           p = p->next ;
           if ( p->next!=NULL )
           { q = p->next ;
           p->next = p->next->next ;
           free (q) ;
           printf("输出所有学生的信息:\n");
           displayAll(head);
           }
           else
           return  NULL  ;

       }
       int  main()
       {   linklist *L,*head,*p;
        long no;
        char name[20];
         char sex[5];
         int age;
         int a;
            printf("\t 1创建(带头尾插)\n");
             printf("\t 2指定学号后插入\n");
            printf("\t 3按学号删除\n");
            printf("\t 4计算学生总数\n");
            printf("\t 5输出学生信息\n");
             printf("\t 6按姓名查找\n");
            printf("\t 7退出\n");
        printf("\n请输入功能选项:");
        scanf("%d",&a);
         switch(a)
         {
         case 1:L=creatlist(); break;

         case 2:{printf("\n输入欲在哪个学号后插入数据:");
         scanf("%s",no);
                p=(linklist *)malloc ( sizeof( linklist)) ;
                printf("\n请输入学号:");
                scanf("%ld",p->data.no);
                printf("\n请输入姓名:");
                scanf("%s",p->data.name);
                printf("\n请输入性别:");
                scanf("%s",p->data.sex);
                printf("\n请输入年龄:");
                scanf("%d",p->data.age);
                insert(L,no);

         break; }

         case 3:{printf("\n输入欲删除学生的学号:");
         scanf("%ld",no);
         dellist(L,no);
         break;  }

         case 4:{printf("\n学生总数为:%d\n",listlength(L));
             break; }

         case 5:{printf("\n输出学生信息:%s\n");displayAll(L) ;
         break; }

         case 6:{printf("\n输入欲查找学生的姓名");
         scanf("%s",name);
         locate(L,name);break;

         case 7:{printf("\n退出\n");break;}
         default:printf("请重新输入\n");
        
         }
         }
       }   
这一个程序运行后也出现exe停止工作,求高手解答原因
2013-11-10 16:31
zhaogay
Rank: 7Rank: 7Rank: 7
来 自:宫
等 级:黑侠
帖 子:151
专家分:586
注 册:2013-10-10
得分:3 
程序代码:
#include<stdio.h>
       #include<malloc.h>
       #include<string.h>
       #include<stdlib.h>
        typedef struct{
         long no;
         char name[20];
         char sex[5];
         int age;
         }student;
         typedef struct node
          {student data;
           struct node *next;
          }linklist;
          linklist *input()          /*输入学生信息*/
      {
          linklist *p;
          p =(linklist *)malloc (sizeof (linklist ));
          printf("\n 请输入学号:");
          scanf("%ld",&p->data.no);//
          printf("\n 请输入姓名:");
         scanf("%s",p->data.name);
          printf("\n 请输入性别:");
          scanf("%s",p->data.sex);//
          printf("\n 请输入年龄:");
          scanf("%d",&p->data.age);
          return p;
       }
         void  displayAll(linklist *L)  /*显示学生信息*/
         {linklist *p;
         p=L->next;
         printf("输入学生的学号、姓名、性别、年龄\n");
         while(p!=NULL)
         {printf("\n%10ld",p->data.no);//
         printf("\t\t");
         printf("%s",p->data.name);
         printf("\t\t");
         printf("%s",p->data.sex);p=p->next;
         printf("\t\t");
         printf("%d\n",p->data.age);
         }
         }
          linklist *creatlist()  /*尾插法建表*/
     {       int i=0;
              linklist *s,*r,*p ;
              p=(linklist *)malloc(sizeof(linklist));
              r = p ;
         while(i==0)
         { s=input();
          r->next=s;
          r=s;
          printf("\n如果想继续输入,请输入0 \n");
          scanf("%d",&i);
          }
          r->next=NULL;
          displayAll(p);
          return p;
          }
       int listlength(linklist *head)  /*计算表长*/
        {
        int j;
        linklist *q;
         q=head->next;
         j=0;
         while(q!=NULL)
         {q=q->next;
         j++;
         }
         return j;
        }
        linklist  *locate( linklist  *head ,char name[20] ) /*以姓名查找*/
       {
           linklist  *s;
           s= head->next;
           while ( s!= NULL )
              if ( s->data.name!=name)
                  s = s->next;
             else
                  break ;
           return  s;
        }
        void insert (linklist *head, long no)/*在指定学号后插入学生信息*/
        {
            linklist  *q,*s,*r;
            q=head;
            while(q->data.no!=no)
             q=q->next;
             s =(linklist *)malloc (sizeof( linklist)) ;
             r=input();
             s->next=q->next;
             q->next=s;
        }
      
            linklist *dellist(linklist *head,long no)  /*删除指定学号信息*/
        {
            linklist  *p,*q;
            p = head ;
            while (( p->next != NULL)&& (p->data.no!=no))
            p = p->next ;
            if ( p->next!=NULL )
            { q = p->next ;
            p->next = p->next->next ;
            free (q) ;
            printf("输出所有学生的信息:\n");
            displayAll(head);
            }
            else
            return  NULL;
        }
        int  main()
        {   linklist *L,*p;//
         long no;
         char name[20];
          //char sex[5];//
          //int age;//
          int a;
             printf("\t 1创建(带头尾插)\n");
              printf("\t 2指定学号后插入\n");
             printf("\t 3按学号删除\n");
             printf("\t 4计算学生总数\n");
             printf("\t 5输出学生信息\n");
              printf("\t 6按姓名查找\n");
             printf("\t 7退出\n");
         printf("\n请输入功能选项:");
         scanf("%d",&a);
          switch(a)
          {
          case 1:L=creatlist(); break;
          case 2:{printf("\n输入欲在哪个学号后插入数据:");
          scanf("%ld",&no);//
                 p=(linklist *)malloc ( sizeof( linklist)) ;
                 printf("\n请输入学号:");
                 scanf("%ld",&p->data.no);//
                 printf("\n请输入姓名:");
                 scanf("%s",p->data.name);
                 printf("\n请输入性别:");
                 scanf("%s",p->data.sex);
                 printf("\n请输入年龄:");
                 scanf("%d",&p->data.age);//
                 insert(L,no);
          break; }
          case 3:{printf("\n输入欲删除学生的学号:");
          scanf("%ld",&no);//
          dellist(L,no);
          break;  }
          case 4:{printf("\n学生总数为:%d\n",listlength(L));
              break; }
          case 5:{printf("\n输出学生信息:\n");displayAll(L);//
          break; }
          case 6:{printf("\n输入欲查找学生的姓名");
          scanf("%s",name);
          locate(L,name);break;
          case 7:{printf("\n退出\n");break;}
          default:printf("请重新输入\n");
       
          }
          }
          return 0;
        }
       后面带//是改过的,能不能跑楼主试下。

好好学习,天天想上
2013-11-10 17:02
听阳光洒下
Rank: 1
等 级:新手上路
帖 子:51
专家分:0
注 册:2013-11-10
得分:0 
回复 5楼 zhaogay
运行到第二个:在指定学号后插入就exe停止工作了
2013-11-10 18:51
逆风而前
Rank: 7Rank: 7Rank: 7
来 自:福建
等 级:黑侠
威 望:7
帖 子:193
专家分:567
注 册:2013-2-14
得分:17 
#include<stdio.h>
       #include<malloc.h>
       #include<string.h>
       #include<stdlib.h>
        typedef struct{
         long no;
         char name[20];
         char sex[5];
         int age;
         }student;
         typedef struct node
          {student data;
           struct node *next;
          }linklist;
          linklist *input()          /*输入学生信息*/
      {
          linklist *p;
          p =(linklist *)malloc (sizeof (linklist ));
          printf("\n 请输入学号:");
          scanf("%ld",&p->data.no);//
          printf("\n 请输入姓名:");
         scanf("%s",p->data.name);
          printf("\n 请输入性别:");
          scanf("%s",p->data.sex);//
          printf("\n 请输入年龄:");
          scanf("%d",&p->data.age);
          return p;
          }
          void  displayAll(linklist *L)  /*显示学生信息*/
          {
              linklist *p;
              p=L->next;
              printf("输入学生的学号、姓名、性别、年龄\n");
              while(p!=NULL)
              {
                  printf("\n%10ld",p->data.no);//
                  printf("\t\t");
                  printf("%s",p->data.name);
                  printf("\t\t");
                  printf("%s",p->data.sex);
                  printf("\t\t");
                  printf("%d\n",p->data.age);
                  p=p->next;
              }
          }
          linklist *creatlist()  /*尾插法建表*/
          {      
              int i=0;
              linklist *s,*r,*p ;
              p=(linklist *)malloc(sizeof(linklist));
              r = p ;
              while(i==0)
              {
                  s=input();
                  r->next=s;
                  r=s;
                  printf("\n如果想继续输入,请输入0 \n");
                  scanf("%d",&i);
              }
              r->next=NULL;
              displayAll(p);
              return p;
          }
       int listlength(linklist *head)  /*计算表长*/
        {
        int j;
        linklist *q;
         q=head->next;
         j=0;
         while(q!=NULL)
         {q=q->next;  
         j++;
         }
         return j;
        }
     
       linklist  *locate( linklist  *head ,char name[20] ) /*以姓名查找*/
       {
           linklist  *s;
           s= head->next;
           while ( s!= NULL )
              if ( s->data.name!=name)
                  s = s->next;
             else
                  break ;
           return  s;
        }
        
        void insert (linklist *head, long no)/*在指定学号后插入学生信息*/
        {
            linklist  *q,*r;
            q=head;
            while(q->data.no!=no)
             q=q->next;
         //    s =(linklist *)malloc (sizeof( linklist)) ;
             r=input();
             r->next=q->next;
             q->next=r;
           //  s->next=q->next;
           //  q->next=s;
        }
      
        linklist *dellist(linklist *head,long no)  /*删除指定学号信息*/
        {
            linklist  *p,*q;
            p = head ;
            while ( (p->data.no!=no)&&( p->next != NULL))
            {
                q=p;
                p=p->next;
            }
               if(p->data.no==no)
               {
                q->next=p->next;
                free(p);
                printf("输出所有学生的信息:\n");
                displayAll(head);
                return head;
               }
               else if(p->next==NULL)
               {
                   return NULL;
               }
        return NULL;
        }
        int  menu(int a,linklist *L)
        {   
    //        linklist *p;//
            long no;
            char name[20];
            //char sex[5];//
            //int age;//
            
            switch(a)
            {
            case 1:L=creatlist(); break;
            case 2:{printf("\n输入欲在哪个学号后插入数据:");
                scanf("%ld",&no);
                insert(L,no);
                break; }
            case 3:{printf("\n输入欲删除学生的学号:");
                scanf("%ld",&no);//
                dellist(L,no);
                break;  }
            case 4:{printf("\n学生总数为:%d\n",listlength(L));
                break; }
            case 5:{printf("\n输出学生信息:\n");displayAll(L);//
                break; }
            case 6:printf("\n输入欲查找学生的姓名");
                scanf("%s",name);
                locate(L,name);break;
            case 7:{printf("\n退出\n");break;}
            default:printf("请重新输入\n");
               
                  
            }
            return 0;
        }
        void main()
        {
            int a;
            linklist *L;
            printf("\t 1创建(带头尾插)\n");
            printf("\t 2指定学号后插入\n");
            printf("\t 3按学号删除\n");
            printf("\t 4计算学生总数\n");
            printf("\t 5输出学生信息\n");
            printf("\t 6按姓名查找\n");
            printf("\t 7退出\n");
            printf("\n请输入功能选项:");
            L=creatlist();
            scanf("%d",&a);
            while(a!=7)
            {

            menu(a,L);
            printf("\t 1创建(带头尾插)\n");
            printf("\t 2指定学号后插入\n");
            printf("\t 3按学号删除\n");
            printf("\t 4计算学生总数\n");
            printf("\t 5输出学生信息\n");
            printf("\t 6按姓名查找\n");
            printf("\t 7退出\n");
            printf("\n请输入功能选项:");
            scanf("%d",&a);
            }
        }
2013-11-12 13:24
逆风而前
Rank: 7Rank: 7Rank: 7
来 自:福建
等 级:黑侠
威 望:7
帖 子:193
专家分:567
注 册:2013-2-14
得分:0 
改了一下按提示输入,就不会错
2013-11-12 13:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-423327-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027346 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved