C++问题,程序分析
viod print(struct Student *head){struct Student *p;printf("\nNow,These %d records are:\n",n);p=head;if(head!=NULL)do{printf("%ld%5.1f\n",p->num,p->score);p=p->next;}while(p!=NULL);}
void main(){struct Student *pt;pt=creat();print(pt);}
请问哪里出错了;看了半天没看出为啥,, 展开
清楚了我输入数字的时候中间有英文的逗号
很多人非常粗心 我让看我的 硬是对我输入时的逗号视而不见
睁着眼睛说不对
# include <stdio.h>
# include <malloc.h>
# define LEN sizeof(struct Student)
struct Student
{
int num;
float score;
struct Student *next;
};
int n;
struct Student *creat()
{
struct Student *head;
struct Student *p1,*p2;
n=0;
p1=p2=(struct Student *) malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score); //%d,%f
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct Student *)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score); //%d,%f
}
p2->next=NULL;
return(head);
}
void print(struct Student *head) //void
{
struct Student *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%d %5.1f\n",p->num,p->score); //%d
p=p->next;
}
while(p!=NULL);
}
void main()
{
struct Student *pt;
pt=creat();
print(pt);
}
void print(struct Student *head)