user-0 提问: 小白希望大佬们能帮帮忙看看程序错误

回复:
user: user-1
content: #include<stdlib.h> #include<string.h> #include<stdio.h>  #define length_of_number 20 #define length_of_name 15 #define length_of_caozuo 30  #define false 0 #define true 1  typedef struct Stumath {     char number[length_of_number];     char name[length_of_name];     int math;     struct Stumath *Next; }Stu;  struct Stumath *pstheadnod=NULL;  Stu *Find_student_by_number(char *pcnumber) {     Stu *Current=NULL;      //if(Current==NULL)     if (pstheadnod == NULL)     {         return false;     }      Current=pstheadnod;     while (Current)     {         if(0==strncmp(pcnumber,Current->number,length_of_number))         {             break;         }         Current = Current->Next;     }      return Current; }  int check_insert_student(Stu *Stnumber) {     Stu *pNow;     if(Stnumber==NULL)     {         printf("学生信息为空\n");         return false;     }     if(pstheadnod==NULL) {         pstheadnod=Stnumber;         return true;     }        pNow=pstheadnod;         while (pNow->Next)         {             pNow = pNow->Next;         }         pNow->Next=Stnumber;     return true; }  int insert_data() {     Stu Studata;     Stu *pstdata=NULL;     memset(&Studata,0,sizeof(Stu));     printf("请输入学生信息:\n");     scanf("%s %s %d",Studata.number,Studata.name,&Studata.math);     if(NULL != Find_student_by_number(Studata.number))     {         printf("已经有人使用学生信息，请重新输入:\n");         return false;     }     pstdata=(Stu *)malloc(sizeof(Stu));     if(pstdata==NULL)     {         printf("系统内存不足\n");         return false;     }     memset(pstdata,0,sizeof(Stu));     strncpy(pstdata->number,Studata.number,length_of_number);     strncpy(pstdata->name,Studata.name,length_of_name);     pstdata->math=Studata.math;     if(false==check_insert_student(pstdata))     {         free(pstdata);         printf("插入数据错误\n");         return false;     }     printf("插入成功\n");     return true; } int delete_by_name(char *pcname) {     Stu *before=NULL;     Stu *Dangqian=NULL;     if(pcname==NULL)     {         printf("名字为空\n");         return false;     }     if(pstheadnod==NULL)     {         printf("系统为空，无法删除\n");         return false;     }     if(0==strncmp(pstheadnod->name,pcname,length_of_name))     {         Dangqian=pstheadnod;         pstheadnod=pstheadnod->Next;         free(Dangqian);         return true;     }     Dangqian=pstheadnod->Next;     before=pstheadnod;     while(Dangqian)     {         if(0==strncmp(Dangqian->name,pcname,length_of_name))         {             before->Next=Dangqian->Next;             free(Dangqian);             return true;         }         else         {             before=Dangqian;             Dangqian=Dangqian->Next;         }     }     return false; } int delete_student() {     char Stname[length_of_name];     memset(Stname,0,length_of_name);     printf("请输入要删除的学生姓名:\n");     scanf("%s",Stname);     if(false==delete_by_name(Stname))     {         printf("删除失败\n");         return false;     }     printf("删除成功，名字是:%s\n",Stname);     return true; } int print_data() {     Stu *present=NULL;     present = pstheadnod;     if (!present) {         printf("系统为空\n");         return 0;     }     while(present) {         printf("%s,%s,%d\n",present->number,present->name,present->math);         present=present->Next;         printf("打印成功\r\n");     }      return 1; } /*int input_test()   {   Stu stEmInfor;   Stu *pstEmInfor=NULL;   memset(&stEmInfor,0,sizeof(Stu));   scanf("%s %s %d",stEmInfor.number,stEmInfor.name,&stEmInfor.math);   printf("%s%s%d",stEmInfor.number,stEmInfor.name,stEmInfor.math);   return true;   }*/ int main() {     char caozuo[length_of_caozuo];     while(1)     {         printf("请输入操作命令:\n");         memset(caozuo,0,length_of_caozuo);         //gets(caozuo);         scanf("%s", caozuo);         if(0==strncmp(caozuo,"add",strlen("add")))         {             insert_data();         }         else if(0==strncmp(caozuo,"delete",strlen("delete")))         {             delete_student();         }         else if(0==strncmp(caozuo,"print",strlen("print")))         {             print_data();         }         else if(0==strncmp(caozuo,"quit",strlen("quit")))         {             break;         }         else if(0==strncmp(caozuo,"exit",strlen("exit")))         {             break;         }         else             printf("输入命令错误\n");     }     return 0; }   供参考～

user: user-2
content: 这是由于scanf会取换行符之前的内容保存下来，但这个换行符依旧留在输入的队列里。也就是说运行至下一次scanf或gets时，它就直接识别到了了这个换行符，并读取了一个空的字符串，这就造成你碰到的错误。  解决方法就是在scanf 后面加一个getchar 抓取走着个换行符就行了。       scanf("%s %s %d", Studata.number, Studata.name, &Studata.math);     getchar();