user-0 提问: goto quit 中的getchar问题

回复:
user: user-1
content: #include<stdio.h> #include<string.h> #include<stdlib.h>  struct  aa{     char name[100];     char phone[100];     char address[100]; } company[100],person[100];  int com_idx, per_idx;  static void input_company(void) {     int i = com_idx;      scanf("%s %s %s", company[i].name,company[i].phone,company[i].address);     com_idx++; }  static void input_person(void) {     int i;      for(i = per_idx;i < 5; i++)         scanf("%s %s %s",person[i].name,person[i].phone,person[i].address);     per_idx += 5; }  static void del_company(void) {     char com_name[32];     int i, j;      printf("请输入公司的名字:\n");     scanf("%s", com_name);      for (i = 0; i < com_idx; i++) {         if (strcmp(com_name, company[i].name))             continue;         for (j = i; j < com_idx-1; j++)             company[j] = company[j+1];         break;     }     if (i < com_idx) {         com_idx--;         return;     }      printf("Not find company name: %s!\n", com_name); }  static void del_person(void) {     char name[32];     int i, j;      printf("请输入个人名字:\n");     scanf("%s", name);      for (i = 0; i < per_idx; i++) {         if (strcmp(name, person[i].name))             continue;         for (j = i; j < per_idx-1; j++)             person[j] = person[j+1];         break;     }     if (i < per_idx) {         per_idx--;         return;     }      printf("Not find name: %s!\n", name); }  static void mod_company(void) {     char com_name[32];     int i;      printf("请输入要修改的公司的名字:\n");     scanf("%s", com_name);      for (i = 0; i < com_idx; i++) {         if (strcmp(com_name, company[i].name))             continue;         break;     }     if (i < com_idx) {         scanf("%s %s", company[i].phone, company[i].address);         return;     }      printf("Not find company name: %s!\n", com_name); }  static void mod_person(void) {     char name[32];     int i;      printf("请输入要修改的个人名字:\n");     scanf("%s", name);      for (i = 0; i < per_idx; i++) {         if (strcmp(name, person[i].name))             continue;         break;     }     if (i < per_idx) {         scanf("%s %s", person[i].phone, person[i].address);         return;     }      printf("Not find name: %s!\n", name); }  static void find_company(void) {     char com_name[32];     int i;      printf("请输入要查询的公司名字:\n");     scanf("%s", com_name);      for (i = 0; i < com_idx; i++) {         if (strcmp(com_name, company[i].name))             continue;         break;     }     if (i < com_idx) {         printf("%s %s %s", company[i].name, company[i].phone, company[i].address);         return;     }      printf("Not find company name: %s!\n", com_name); }  static void find_person(void) {     char name[32];     int i;      printf("请输入要查询个人名字:\n");     scanf("%s", name);      for (i = 0; i < per_idx; i++) {         if (strcmp(name, person[i].name))             continue;         break;     }     if (i < per_idx) {         printf("%s %s %s\n", person[i].name, person[i].phone, person[i].address);         return;     }      printf("Not find name: %s!\n", name); }  int main() {     int opt, choice;     char pwd[] = "12345", input_pwd[8];       while (1) {         printf("管理员or顾客(管理员请按1,顾客按其他)\n");         scanf("%d", &opt);         if (opt == 1) {             printf("请输入密码:\n");             scanf("%s", input_pwd);             if (strcmp(input_pwd, pwd))                 continue;              printf("密码正确");             printf("进入管理员系统\n");             printf("选择 录入(1) 删除(2) 修改(3)\n");             scanf("%d", &opt);             switch (opt) {                 case 1:                     printf("录入公司(1)or个人信息(其他)\n");                     scanf("%d", &choice);                     printf("开始录入:\n");                     if (choice == 1)                         input_company();                     else                         input_person();                     break;                  case 2:                     printf("请输入要删除个人信息(1)还是公司信息(其他)\n");                     scanf("%d", &choice);                     if (choice == 1)                         del_person();                     else                         del_company();                     break;                  case 3:                     printf("修改公司信息(1)or个人信息(其他)\n");                     scanf("%d", &choice);                     if (choice == 1)                         mod_company();                     else                         mod_person();                     break;                  case 4:                     printf("Not support!\n");                     break;             }         } else if (opt == 2) {             printf("欢迎进入顾客系统\n");             printf("选择进入公司or个人(公司按1,个人按其他)\n");             scanf("%d", &choice);             if (choice == 1)                 find_company();             else                 find_person();         } else {             continue;         }     } }    楼主的代码有问题，不建议用getchar();而是用scanf %d，即用int类型选择  上面的代码没测试，建议楼主试试，有问题再提出来～

user: user-2
content: 前面思想是对的，getchar把回车读走，难你case里也要把读走，你把循环里的也getchar一下试试。练习可以，真正写的话能循环就循环，尽量避免用goto。教我们的老师说容易导致程序崩掉

user: user-3
content: 是上一轮的scanf后遗留在缓冲区的回车没有清理 scanf后getchar获得的是之前的回车键

user: user-1
content: getchar()接收一个字符，输入11，但是也是分两次给getchar()