簡體   English   中英

在這種特殊情況下,為什么我的程序兩次打印此消息?

[英]Why is my program printing out this message twice in this particular situation?

我編寫了一個程序,該程序將根據您輸入的命令打印輸出。 所有功能均正確/所有輸出均正確。

我正在嘗試提供檢查以查看是否:

  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;
}

[edit]推薦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)讀取第一個K K與任何命令都不匹配。 while循環到來,輸出提示並消耗第二個K 同樣的事情發生,直到第三個K被消耗。

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

[編輯]一種簡單的方法

您還具有UB作為char command; 沒有為command分配值- 可能Q ,程序將立即退出。 建議使用do { ... } while ()循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM