0

Okay, so I have a for loop that checks through every letter of a string to make sure that the string is made entirely of alphabetical characters before moving on to the next stage of the program.

However, I am having trouble with my if statement that is supposed to run if the "isalpha" function comes back false.

string keyword = argv[1];
for (int i = 0; i < strlen(keyword); i++)
{
    char letter = keyword[i];
    if (isalpha(letter) = false)
    {
        printf("Only use alphabetical characters in your key\n");
        return 1;
    }

I am getting the error:

error: expression is not assignable if (isalpha(letter) = false)

How can I get an if statement that initiates due to a isalpha being false?

How is the following wrong?

if (isalpha(letter) = false)
Tom
  • 21
  • 1
  • 1
  • 8
    It's `==`. Or just `!isalpha(letter)`. – Eugene Sh. Jul 19 '17 at 20:19
  • We're not human debuggers, you should fix compiler problems yourself. – simo-r Jul 19 '17 at 20:20
  • 3
    @BetaRunner It's not even debuggers, but error message parsers... – Eugene Sh. Jul 19 '17 at 20:22
  • Indeed looks stupid, but in favor of the OP, if his compiler really shows the cited message, this is not entirely clear just showing the whole line... –  Jul 19 '17 at 20:33
  • @FelixPalmen - perfectly clear (to me) – KevinDTimm Jul 19 '17 at 20:42
  • @KevinDTimm it doesn't state what the "expression" is, that's not "assignable". I don't say this couldn't have been solved with just some "*google skills*", still, coming from another language where `=` compares things and seeing *this* error message talking about the whole line as an expression that is not assignable -- it's at least not immediately obvious. –  Jul 19 '17 at 20:43
  • Classic case where yoda programming might have helped, given the odd message OP got from his compiler. – Nate Jul 19 '17 at 21:27
  • 1
    The C tag. IMO the "recently" added boolean type was a wrong turn for C. It is completely unnecessary. – Weather Vane Jul 19 '17 at 21:57
  • 1
    @WeatherVane yeah, why don't we just program universal turing machines with tape? – M.M Jul 20 '17 at 01:57

2 Answers2

3

You made a typo: if (isalpha(letter) = false) is not a comparison, but an assignment. You should instead write:

if (isalpha(letter) == false)

A more common way to write this is with the ! operator:

if (!isalpha(letter))

Note however that isalpha() has undefined behavior for negative values different from EOF, and if the char type is signed, the value if letter can be negative. This problem is corrected with a cast:

if (!isalpha((unsigned char)letter))
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Yes, but `isalpha` and cousins take `int`. – Weather Vane Jul 19 '17 at 22:00
  • @WeatherVane: `isalpha()` and friends from `` take an `int`, but are only defined for all values of type `unsigned char` and the special negative value `EOF`. A negative `char` argument is promoted to `int` with the same value, and unless it happens to be the same as `EOF`, the behavior is undefined. If the `char` has the same value as `EOF`, the behavior is defined but potentially incorrect: `'ÿ'` has value `-1` for character set ISO-8859-1, will be treated as `EOF` which is commonly defined to the same value. It is basically the same problem as `char c = getchar();`. – chqrlie Jul 19 '17 at 23:31
  • The value of the character must be passed as an `unsigned char` for it to be meaningful. `char` can be signed by default, but whenever character values are used in the C library, it is the value of the `unsigned char` that is considered: `strcmp()` compares the string bytes as converted to `unsigned char`, like `memcmp()` for which it is less questionable, `getchar()` returns the value of a byte from the stream as an `unsigned char`, `putchar()` converts its argument to an `unsigned char` and returns that... All functions from `` need unsigned character values or the value `EOF`. – chqrlie Jul 19 '17 at 23:44
1

How is the following wrong?

if (isalpha(letter) = false)

Well, it's an assignment! = is used to assign a value, and you can't assign a value to a function call (for hopefully obvious reasons)

To compare values, you use the == operator in C.

That said, checking for equality with == is not necessary at all when you already have boolean values. Just write:

if (!isalpha(letter))