c - 为什么我的程序在这种特殊情况下会两次打印此消息?

标签 c printf

我编写了一个程序,它将根据您输入的命令打印输出。所有功能均正确/所有输出均正确。

我正在尝试提供检查是否:

  1. 文件格式错误(如果是,请退出程序)
  2. 用户输入的搜索命令具有不同的语法(如果是这样,请返回命令提示符)但是,我不太确定如何实现这些检查!

我遇到的另一个问题是,当我输入“KK”或“RR”或任何不是指定命令的内容时,它会多次打印出消息“输入命令或输入‘Q’退出:”我输入了一个角色。 (即,如果我输入 KKK,它将打印该消息三次;如果我输入 KK,它将打印该消息两次)

用户输入的命令应该如下所示:
注意: 用户没有输入"</>"符号。

S <lastname> 
T <lastname>
G <number>
L <number>
Q

这是我的主要代码:

int main() {
   studentType s[MAX_STUDENTS];
   teacherType t[MAX_TEACHERS];
   char input[MAX_NAME];
   int numS, numT;
   char command;
   FILE * out;

   numS = readStudentInfo(s); /* Number of students equals size of array. */
   numT = readTeacherInfo(t); /* Number of teachers equals size of array. */


   if (numS > MAX_STUDENTS) { /*If # of s exceed maximum, quit program. */
      printf("Number of student exceeds the maximum number allowed.\n");
      return 0;
   }

   if (numT > MAX_TEACHERS) { /* If # of t exceed maximum, quit program. */
      printf("Number of teachers exceeds the maximum number allowed.\n");
      return 0;
   }

   out = fopen("log.out", "w");


   while (command != 'Q') {
      printf("Enter command or enter 'Q' to quit:\n");
      scanf(" %c", &command);

      if (command == 'S') {
         scanf("%s", input);
         fprintf(out, "-->%c %s\n", command, input);
         getStudentInfo(s, t, input, numS, numT, out);
      }

      if (command == 'T') {
         scanf("%s", input);
         fprintf(out, "-->%c %s\n", command, input);
          findStudents(s, t, input, numS, numT, out);
      }

      if (command == 'G') {
         scanf("%s", input);
         fprintf(out, "-->%c %s\n", command, input);
         getGradeList(s, t, atoi(input), numS, numT, out);
      }

      if (command == 'L') {
         scanf("%s", input);
         fprintf(out, "-->%c %s\n", command, input);
         findGradeTeachers(s, t, atoi(input), numS, numT, out);
      }
   }

   if (command == 'Q') {
      fprintf(out, "-->%c\n", command);
      fclose(out);
      return 0;
   }

   return 0;
}

最佳答案

[编辑] 建议放弃 scanf("%c", &command); 和各种 scanf("%s", input);。请改为使用 fgets() sscanf(),如下所示。还可以进行其他简化,但这是为了让 OP 开始。

char buf[MAX_NAME + 4];
int number;
while (fgets(buf, sizeof buf, stdin) != NULL) {
  if (sscanf(buf, "S %s", input) == 1) {
    fprintf(out, "-->S %s\n", input);
    getStudentInfo(s, t, input, numS, numT, out);
  }
  else if (sscanf(buf, "G %d", &number) == 1) {
    fprintf(out, "-->G %d\n", number);
    getGradeList(s, t, number, numS, numT, out);
  }
  ...
  else if (buf[0] == 'Q') {
    fprintf(out, "-->Q\n");
    break;
  }
  else {
    fprintf(out, " X Bad command '%s'\n", buf);
    fclose(out);
    exit(1);
  }
}
fclose(out);

当输入“KKK”时,scanf("%c", &command) 读取第一个 KK 不匹配任何命令。 while 循环出现,打印提示并消耗第二个 K。同样的事情再次发生,直到第 3 个 K 被消耗。

while (command != 'Q') {
  printf("Enter command or enter 'Q' to quit:\n");
  scanf(" %c", &command);
  if (command == 'S') {
     ...
  }
  ...
}

[编辑] 一个简单的方法

您还可以将 UB 作为 char 命令; 不会为 command 赋值 - 它可能Q 并且程序会立即退出。建议使用 do { ... } while () 循环。

关于c - 为什么我的程序在这种特殊情况下会两次打印此消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20436394/

相关文章:

c++ - 十六进制数组 cout 到文本文件失败

c - 在 C 中处理交互式输入/输出应该是什么样子的?

将 C 转换为 mips 程序集,未对齐的地址错误

c - GCD 中的定时锁?

c - 在 printf ( "%d", 5 ); 5存储在内存中吗?

c++ - c中的uint4变量值打印

带有函数指针的编译时检查结构以验证分配

c - 如何用整数中的另一组半字节替换给定的半字节

C - 第三次 scanf 修改第二次 scanf 中的变量

c++ - 为什么我的代码中的fprintf()函数无法正常工作?