0

I am at the point in pset2 Vinegere, where I have to write the shift function. The tutorial (or guidance on the left side in sandbox) is telling me to place the following line of code after the curled brace of main:

int shift(char c)

    {
       // TODO
    }

However, when I tried to compile, I got the answer:

vigenere.c:52:1: error: control reaches end of non-void function [-Werror,-Wreturn-type]
}
^

I tried to fill in the //TODO and came to the following code:

int shift(char c)
{
   for (int i = 0; i < c; i++)
       if isupper(shift)
       {
           printf("%i", (shift-65) % 26);
       }
       else if islower(shift)
       {
           printf("%i", (shift-97) % 26);
       }
       else
       {
           printf("%i", shift);
       }
}

However, this seems not to work either.

Can anybody help me on this please? I can't my mistakes.

Kind Regards,

Nisanth

2 Answers 2

1

The compile error is because by definition, the function will return an int, but there is no return in the (blank) version. Remember the parts of the function definition:

int: what type the return value will be
shift: the name of the function
char c: the argument the function expects

Since c is a single character, there is no call for a for loop.

This function seems to confuse the function name (shift) with the function argument (c).

0

Your function is "int":

int shift(char c)

Function must return integer. Not "printf"!

So the output of your case must be:

int shift(char c)
{
    if (isalpha(c))
    {
        if (isupper(c))
        // The ASCII value of A is 65
        {
            c = c - 65;
        }
        else if (islower(c))
        // The ASCII value of a is 97
        {
            c = c - 97;
        }
    }
    else
    {
        return c;
    }
    return c;
}

You must log in to answer this question.

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