运行c++程序时出现.exe已停止工作

这是我的源代码,请求高手指点
//建立动态单链表 用cin,cout程序没法运行
#include<stdlib.h>//利用malloc函数
#include<stdio.h>
#define len sizeof(struct student)
struct student
{
float score;
int num;
struct student *next;
};
void main ()
{
void print(struct student *head);
struct student *creat(void);//声明返回指针值的函数, 说明creat()函数是一指针型函数,即函数返回值是一指针

struct student *pt,*head;
pt=creat();
print(head);
// printf("\nnum:%ld\nscore:%5.lf\n",pt->num,pt->score);//输出第一个节点的成员值,有返回值决定
}
int n;//节点个数
struct student *creat(void)
{
struct student *p1, *p2,*head;
n=0;

p1=p2=(struct student*)malloc(len);//(struct student *)使malloc返回的指针是struct student类型的,
//*不能少,否则返回的就不是指针了。系统能够实现隐式的转换所以,
//可写成p1=p2=malloc(len)
scanf("%ld,%f",&p1->num,&p1->score );//输入第一个学生的信息
head=NULL;
while(p2->num!=0)//最后节点信息输入完毕后输入0,0以表示信息输入完毕
{
n=n+1;
if (n==1) head=p1;
else p2->next =p1;
p2=p1;

p1=(struct student *)malloc(len);
scanf("%ld,%f",&p1->num,&p1->score );//输入其他学生的信息
}
p2->next =NULL;
return head ;//返回头结点,也可返回其他节点head->next即返回第二节点
}
void print(struct student *head)
{
struct student *p=NULL;
printf("%nNow,these%d records are:\n",n);
p=head;
if (head!=NULL)
do
{
printf("%ld %5.lf\n",p->num,p->score);
p=p->next;
}
while(p!=NULL);
}

发现你程序中最致命的错误有2个地方:
1.main函数中
---------------------------
struct student *pt,*head;
pt=creat();
print(head);
-------------------------
你creat函数返回的头指针应该赋值给head吧,,然后才能print(head)

2.print函数中
--------------------------
printf("%nNow,these%d records are:\n",n);
--------------------------
%n 导致了程序的运行崩溃,,你应该是想写\n吧。。。下次认真点。。。

还有几个可以改进的地方,比如creat函数中完全不需要定义n来判断是否是第一个结点,可以在第一个结点申请后,直接赋值head = p1 = p2。还有你的判断条件while(p2->num!=0)应该改为p1->num!=0,你认真看看是不是?

好的兴趣是成功的一半。。加油。。。

当程序出现问题时,可以用VC的断点Debug功能来观察变量的值,很好用而且是必须要学会用的。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-07-28
某个软件卸载的时候没卸载干净导致的,或者是驱动没卸载干净。解决办法:
开始-运行-MSCONFIG-在启动项中找到NTVDM.EXE并将其前面的勾勾除,重启之后再弹出的对话框中把不在提示的哪个框勾上,确定就好了本回答被网友采纳
第2个回答  2011-04-01
f("%f",&f);

少了一个 '&'
另外,虚机团上产品团购,超级便宜
相似回答