0

I can't fix this logical problem. I have a check_alpha function which should return if all characters are alphabetic.

bool check_alpha(char key[])
{
    for (int i = 0, n = strlen(key); i < n; i++)
    {
        if (isalpha(key[i]))
        {
            printf("everything ok\n");
        }
        else {
            printf("not alpha.\n");
        }
    }
    return false;
}

And before that in my main function I have this condition

 else if (argc == 2 && !(check_alpha(argv[1])))
    {
        printf("Key must only contain alphabetic characters.\n");
        return 1;
    }

With current code and with argv of 26 alphabetic characters, I still get "Key must only contain alphabetic characters." If I change my logic, it still doesn't work properly, doesn't matter what I try. On the other hand, printf statements in my check_alpha function show me that it is working properly. Any help, what am I doing wrong? Ideally, if all 26 characters are letters, it should procede to check next condition...

1

Using multiple expressions in a logical condition like IF always complicates understanding, I would use separate checks, I would also not use a function to check if a character is alpha or not, for that we have the function isalpha () !!. On a positive note, it is good that you dare to experiment with functions. However be careful, let's see your code, the function check_alpha () takes as an argument a string and always returns FALSE, now let's see the IF condition written in English (well, the English that I handle ...):

"if the number of arguments is two and the !check_alpha () function is true, the body of the IF condition is executed"

Let's take a closer look at this, check_alpha () returns FALSE, so !chek_alpha() is true, so if we have two arguments the IF condition is true. I hope it was clear, and it will help you.

6
  • Thanks for your help and suggestions :) if I understood you well, this 'return false' in my check_alpha function is causing the problem, but my program won't compile if I remove it... – ivan milenkovic May 31 '20 at 15:51
  • If the function returns a boolean value, you must make sure that it actually returns it, otherwise you will have a compilation error, as a clue: we must return TRUE when the conditions that interest us are satisfied, but by default we can return a Bool adequate, to not have false positives. It's worth trying for yourself, but if you can't, don't hesitate to post a new question that will be useful to many people. – MARS May 31 '20 at 16:09
  • I made things more complicated, but I finally found the way : ` bool check_alpha(char key[]) { int alpha = 0; for (int i = 0, n = strlen(key); i < n; i++) { if (isalpha(key[i])) { alpha++; printf("there is %i alpha chars", alpha); if (alpha == 26) { return true; } } else { printf("not alpha.\n"); return false; } } return false; } ` – ivan milenkovic May 31 '20 at 16:29
  • And above ` else if (argc == 2 && !(check_alpha(argv[1]))) { printf("Key must only contain alphabetic characters.\n"); return 1; }` with this if else, it finally works as I want, but this is really bad code... – ivan milenkovic May 31 '20 at 16:30
  • Making the code more efficient in C is not always easy, don't worry too much now if it looks ugly, the important thing now is what you have learned – MARS May 31 '20 at 16:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .