-2

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.

Ethan Malloy
  • 555
  • 1
  • 4
  • 16
  • 1
    strEmployeeStatus is a single character, but you use it with string specifier, you need a char array, or %c instead of %s. `strEmployeeStatus != "H"` is also wrong, you are comparing a single character with a string. – anastaciu Feb 20 '21 at 22:29
  • @xing, that didn't work. – Ethan Malloy Feb 20 '21 at 22:35
  • 1
    I don't mean to be rude, but this is the basics of the language, it seems that you are in need of a [good C book](https://stackoverflow.com/q/562303/6865932) – anastaciu Feb 20 '21 at 22:37

2 Answers2

1

Add the correct headers. Then enable compiler warnings and compile it again.

Mainly, you're mixing up string literals ("foobar") with character literals ('x'); this is what GCC has to say about the initialization:

blah.c: In function ‘GetEmployeeStatus’:
blah.c:6:30: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
     char strEmployeeStatus = "";
                              ^~

With similar warnings from the comparisons.

Also consider what the %s conversion for scanf() does. From the Linux man page:

s
Matches a sequence of non-white-space characters; the next pointer must be a pointer to the initial element of a character array that is long enough to hold the input sequence and the terminating null byte ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.

Here, in your code, strEmployeeStatus is just one char. You are giving scanf() a pointer-to-char correctly, though, so the compiler has a hard time warning about that.

char strEmployeeStatus;
scanf("%s", &strEmployeeStatus);

Then, the loop condition: strEmployeeStatus != "H" || strEmployeeStatus != "M". That's (X is not A OR X is not B). What happens in an expression like this when X is A, B, or C? What are the values of the subexpressions, and what's then the value of the whole OR expression in each case?

ilkkachu
  • 6,221
  • 16
  • 30
-1

I talked with someone IRL, and we fixed the problem. The issue wasn't with scanf at all. The problem was my compound OR statement. The line below:

intEmployeeStatusResult == 0 || strEmployeeStatus != "H" || strEmployeeStatus != "M"

will always evaluate to true even if H or M is entered. I changed the line to:

intEmployeeStatusResult == 0 || strEmployeeStatus != "H" && strEmployeeStatus != "M"

and the program works as intended now.

Ethan Malloy
  • 555
  • 1
  • 4
  • 16