user2387766
user2387766

Reputation:

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

I've written a program that will print output based on the command you enter. All the functions are correct/all output are correct.

I'm trying to provide checks to see if :

  1. the file is in the wrong format (if so, exit the program)
  2. the user inputted search command has a different syntax (if so, go back to the command prompt) But, I'm not too sure how to implement these checks!

Another problem I'm having is when I input "KK" or "RR" or anything that's not a specified command, it will print out the message "Enter command or enter 'Q' to quit:" how ever many times I've entered a character. (i.e. If i put KKK, it will print out that message three times; if I put KK it will print out the message twice)

Here is what the user inputted command SHOULD look like:
NOTE : The user doesn't input the "</>" symbols.

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

Here's my code in main:

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

Upvotes: 0

Views: 1143

Answers (1)

chux
chux

Reputation: 153478

[edit] Recommend dispensing with the scanf(" %c", &command); and various scanf("%s", input);. Instead use fgets() sscanf() as follows. Other simplifications possible, but this to get OP a start.

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

When "KKK" is entered, scanf(" %c", &command) reads the first K. K does not match any command. The while loop comes around, prints the prompt and consumes the 2nd K. Again same thing happens until the 3rd K is consumed.

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

[edit] A simple way to

You also have UB as char command; does not assign a value to command — it could be Q and the program would promptly quit. Suggest using a do { ... } while () loop.

Upvotes: 1

Related Questions