单独一个getchar();是什么用法~?
程序代码:
#include<stdio.h> #include<stdlib.h> struct stud { long num; char name[20]; struct stud * next; }; void new_stud(); void listall(); struct stud *head,*cur,*newp; int main(char argc,char** argv ) { char ch; int isClose=1; head=NULL; while(isClose) { printf("\n type 'E' or 'e' to enter new student,"); printf("\n type 'L' or 'l' to list all students:"); ch=getchar(); getchar(); /*这个getchar() 有什么作用~?*/ switch(ch) { case 'e': case 'E':new_stud();break; case 'l': case 'L':listall();break; default:isClose=0; }; } return 0; } void new_stud() { char numstr[20]; newp=(struct stud *)malloc(sizeof(struct stud)); if(head==NULL) { head=newp; } else { while(cur->next!=NULL) { cur=cur->next; } cur->next=newp; } cur=newp; printf("\n enter NO.:"); gets(numstr); cur->num=atol(numstr); printf("\n enter name:"); gets(cur->name); cur->next=NULL; } void listall() { int i=0; if(head==NULL) { printf("\n No Student!"); return; } cur=head; for(;cur!=NULL;) { printf("\n============================="); printf("\n Num: %ld",cur->num); printf("\n Name: %s",cur->name); printf("\n------------------------"); cur=cur->next; } }