How to compare a value taken from user with fgets with a known char eg 'a'?

By croninjm ·
Hey there,

Im new to c and im struggling with pointers.
I want to check that input from the user is a, f, s, t, or q.

This is what ive got so far..

#include <stdio.h>
#include <string.h>
FILE *file;
char mode;
int main ()
char *i[2];
file = fopen ("students.dat", "r");
if (file == NULL)
exit (8);
int ch, records = 0;
while ( (ch = fgetc(file)) != EOF )
if ( ch == "#" )

printf("There are %d records in the student database.\n a. List all students.\n f. List first year students.\n s. List second year students.\n t. List final year students.\n -----------------------------------\n q. Quit.\n", records);
fgets(i, sizeof(i), stdin);
if ( (i !="a", i) && ("%s" !="f", i) && ("%s" !="s", i) && ("%s" !="t", i) && ("%s" !="q", i) )
printf("Error incorrect input!\n");
return 0;

The problem im having is finding a way to check that the user input is correct. I know fgets returns a pointer which points to the location of where the input is stored, so how do i use that to compare with eg 'a'?

Thanks any help would be greatly appreciated.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Related Discussions

Related Forums