I have a C program that is supposed to evaluate whether the user entered a H
or a M
. I have some input validation to ensure that the user enters either H
or M
. However, when those are entered, the procedure treats it like the user entered something else. I have a feeling that this is because scanf
is adding a newline character to the input which causes it to fail the comparison. I'm trying to find ways to strip out this newline character, but I've had no luck. I've tried putting getchar()
after the scanf
, but that didn't work. I've tried using fgets
and that didn't work either. I tried changing the format indicator from %s
to %c
and that didn't work.
Here is the entire procedure:
char GetEmployeeStatus()
{
// declare variable
char strEmployeeStatus = "";
int intEmployeeStatusResult = 1; // assume valid input
do
{
// get user input
printf("Please enter the employee's status: H for hourly or M for management. \n");
intEmployeeStatusResult = scanf("%s", &strEmployeeStatus);
} while (intEmployeeStatusResult == 0 || strEmployeeStatus != "H" || strEmployeeStatus != "M");
return strEmployeeStatus;
}
Thank you for any help, I really appreciate it.