0

I´m trying to solve this pset but I got stuck with function declaration. I did everything according to the tutorial but the clang keeps showing an error: "control reaches end of non-void function" on the very last line. Also I don´t quite get the declaring of for loop. Firsty, I use a comma and then two semicolons. Why not all either of them? I was a bit stuck because of this. Thank you

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int shift(char c);

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./vigenere keyword\n");
        return 1;
    }



    for (int i = 0, n = strlen(argv[1]); i < n; i++)
    {
        if isalpha(argv[1][i])
        {
            int key = shift(argv[1][0]);
            printf("%i\n", key);
            return 0;
        }
        else
            {
            printf("Usage: ./vigenere keyword\n");
            return 1;
            }
    }

}

int shift(char c)
{
    // TODO     
}
1

Re: the for loop. A for loop structure has 3 sections: initialization, test, increment. Each section is separated by semicolons. Within each section, there can be multiple parts. Those parts are separated by commas. In this case, there are two initializations, for i and n. They are separated by the comma.

"Control reaches end of non-void function" means your function has a path through it where the function ends without actually returning data of the type declared in the function declaration. In this case, your shift() function is declared to return an int. Since it has no return statement (in fact, no code at all), it isn't returning an int, so the compiler throws an error. You need to write something in the code for shift() that will return an int. Simplest thing to do would be to actually write the function at this point. It's a simple function.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

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