程序疑惑
#include <stdlib.h>
#include <stdio.h>
struct studtype
{
char studname[20];
long studnum;
int studage;
char studsex;
float studscore;
struct studtype *nextnode;
}
void appendnewnode(void);
void listall(void);
struct studtype *head=NULL,*thisnode,*new;
main()
{
char ch;
int flag=1;
while(flag){
printf( "\ntype 'E ' or 'e ' to append new code, ");
printf( "type 'L ' or 'l ' to list all: ");
ch=getchar();getchar();
switch(ch)
{
case 'e ':case 'E ':appendnewnode();break;
case 'l ':case 'L ':listall();break;
default:flag=0;
}
}
}
void appendnewnode(void)
{
char numstr[20];
new=(struct studtype *)malloc(sizeof(struct studtype));
if(head==NULL) head=new;
else{thisnode=head;
while(thisnode-> nextnode!=NULL)
thisnode=thisnode-> nextnode;
thisnode-> nextnode=new;
}
thisnode=new;
printf( "\nenter student name: ");
gets(thisnode-> studname);
printf( "\nenter student number: ");
gets(numstr);
thisnode-> studnum=atol(numstr);
printf( "\nenter student age: ");
gets(numstr);
thisnode-> studage=atoi(numstr);
printf( "\nenter student sex: ");
thisnode-> studsex=getchar();getchar();
printf( "\nenter student score: ");
gets(numstr);
thisnode-> studscore=atof(numstr);
thisnode-> nextnode=NULL;
}
void listall(void)
{
int i=0;
if(head==NULL){printf( "\nempty list.\n ");return;}
thisnode=head;
do{
printf( "\nrecord number:%d\n ",++i);
printf( "student name:%s\n ",thisnode-> studname);
printf{ "student number:%ld\n ",thisnode-> studnum};
printf( "student age:%d\n ",thisnode-> studage);
printf( "student sex:%c\n ",thisnode-> studsex);
printf( "student score:%6.2f\n ",thisnode-> studscore);
thisnode=thisnode-> nextnode;
}while(thisnode!=NULL);
}
程序有点长,辛苦各位了。
问题这样,在用turbo c 2.0编译该程序时,提示说“void appendnewnode(void);”这条语句在定义中有太多的类型,十分不解。
[解决办法]
struct studtype
{
char studname[20];
long studnum;
int studage;
char studsex;
float studscore;
struct studtype *nextnode;
}; ------------> 分号忘记写了.
[解决办法]
后面一个getchar()是用来接收你输入字符之后的回车的
如果不加, gets(thisnode-> studname);这个语句会接收回车,thisnode-> studname这个就为空了