0

I wrote a function to return the shift value for a certain character inputted. Where A, a = 0, B,b =1, etc.

int shift(char c)
{
    int asc;
    if (isupper(c))
    {
        asc = atoi(&c) - 65;
        printf("%d", asc);
        return asc;
    }

    else if(islower (c))
    {
        asc = atoi(&c) - 97;
        printf("%d", asc);
        return asc;
    }

}

However I am getting a "control may reach end of non-void function" when I try to compile it, and I am not sure why. I am both printing and int and returning an int. Can anyone help with this?

1
  • I figured it out. I present two conditions which would be all that is possible since I am first checking if the character is alphabetical. But the compiler does not know this, so I just removed the second if statement so all other cases do the second part od the code. I also changed it to just "return c-65; and return c-97;" respectfully to make it simpler. Found answer on this thread: reddit.com/r/cs50/comments/bhikl5/… – Amanda Ramirez Jun 27 '19 at 14:35
1

You're right. The compiler just knows that there's a possibility of getting to the end of the code without seeing a return statement, so it's unhappy.

However, your fix in the comments has a flaw. What happens when a non-alpha is passed to this function? It will return a bad value because it will be treated as a lower case letter.

Instead, you could have added another else clause to simply return the ascii value.

Finally, you could remove the atoi calls completely. A char can be treated directly as a signed one byte integer.

You must log in to answer this question.

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