0

So, I'm currently working on 'Substitution', and have written the following code to evaluate if a certain array of characters are all alphabetic:

for (int i = 0; i < length; i++)
{
    if (isalpha(key[i]))
    {
        printf("Looks good!\n");
    }
    else
    {
        printf("All characters must be alphabetic.\n");
        return 0;
    }
}

Currently, the only way that it will continue looping through all characters (if they're all alphabetic/when the 'if' statement is true) is if I print something (in this case: 'Looks good!').

I'm not sure what value to return so that the loop continues iterating when true, while also not printing something. I know that 0 should mean false and 1 should mean true, but I don't understand how that fits in with exit codes, where 0 actually means that it ran successfully and non-zero numbers mean that there's an error. I've tried returning all kinds of numbers (including 0 and 1) and variables, but nothing seems to work.

I've gotten enough errors at this point about not having a return value for all paths so I know to always have something return, but I'm not clear yet on what to return in different contexts (like this one).

Edit: I will add that the inclusion of the 'isalpha' function is throwing me off as well. In the CS50 manual pages, it says that "This function returns a non-zero int if c is alphabetical and 0 if c is not alphabetical" so I'm not sure how that also fits into all of this.

1 Answer 1

1

First, since not all the relevant code is here, we have to make some assumptions. First, length = strlen(key). Second, this is in main and not in a function that's called by main. Third, the code uses strcpy to copy argv[1] and not `key=argv[1]'.

Given these assumptions, and hoping that there's nothing else undisclosed that could impact this, the code above should be working correctly. However, it could be simplified.

Sometimes it's better to look for the negative instead of the positive. In this case, the code above is doing both. So, in situations, ask yourself this. Do you take action if it's all alpha or if something is not alpha? In this case, it's the latter. If all the key chars are alpha, the code merely continues from there.

With that in mind, it can be simplified by applying the NOT operator.

if (!isalpha(key[i]))
{
    printf("All characters must be alphabetic.\n");
    return 0;
}

Anytime you're in a situation where it feels like you have to have an empty code block after an if statement, or just don't know what to put in it, but what you really want to put the real code in an else clause, that's a really good indicator to flip the test to the opposite, like I did here. BTW, you could have just had a curly brace set and it would work, but that's the mark of a noob, so don't do it. You could do it as a test to see what happens, or play with the NOT operator, ! . I always recommend writing short test progs to understand how things work. Nothing like seeing what your own experiments do!

About return values: It looks like you've conflated three different return value practices. They have little to nothing to do with each other, except that a value is returned.

The first is the program return value. The accepted best practice is that when a program ends normally, it returns 0 to indicate it completed successfully, as expected. When a program terminates on it's own but because of an unexpected or error condition, it should return a non-zero value. The actual return value is up to the programmer. It's usually to indicate what the problem is, or which part of the code generated the error.

The second is bool values and the interpretion of numbers as bools. Simply put, a 0 is always interpreted as false. Period. End of story. A non-zero, whether positive or negative, is always interpreted as true. On the flip side, if a bool has to be cast as an int, false is always cast as 0 and true as 1.

Finally, a return from a function.

When a function is supposed to return a bool and a numeric value is given, it is evaluated as true or false and then that is returned.

Frequently, a function returns an int. For example, the family of issomething functions will all return a 0 when false. When true though, instead of returning 1, these will return a number that's a power of 2, depending on which function it is. (This appears to be a throwback to assembler, where registers are used and certain operations would set certain bit flags in a register.) So, these functions combine the first two concepts. They always return an int, but the value returned can be interpreted as a bool.

Hopefully, this sorts out your confusion.

2
  • 1
    Hi, Cliff, thanks so much for the response! I apologize for the lack of context/relevant code (your assumptions were correct); this was my first post and I wasn’t sure how much of my code to post. I’ll be sure to include more next time. I really appreciate the specific coding tips, as well as the explanation and clarification on the different return value practices. My understanding of it all is much clearer now so thank you again!
    – mstack
    Commented Jul 10, 2022 at 20:33
  • This is very common with new programmers. Glad you could sort it out. If you're happy with it, please click on the check mark to accept the answer. ;-)
    – Cliff B
    Commented Jul 10, 2022 at 21:39

You must log in to answer this question.

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