In this mini version of my program I am asking for user input. When the input is 'quit' or 'exit' I want the program to exit the while loop. The strcmp function doesn't seem to be working as I expect. I've spent some time looking for answers but can't find the issue. Any ideas?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 100
int main() {
char request[BUFFER_SIZE];
while(strcmp(request, "quit") != 0 && strcmp(request, exit) != 0) {
fgets(request, BUFFER_SIZE, stdin);
}
return 0;
}
fgets
reads the trailing \n
and stores it in the buffer, so you're always comparing quit
to quit\n
.
Also at the first time the while
loop examines its condition, very bad things could happen because your request
array is not initialized.