1

I am getting this error when trying to compile the below code.

"non-void function does not return a value in all control paths"

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

string cipher(string word);

int main(void)

{
    string word1 = get_string("Enter word you want to be encoded.");
    string codedMessage = cipher(word1);
    printf ("Encoded message is: %s", codedMessage);
    
    
}

string cipher(string word)

{
    string finish = ("");
    for(int i = 0 , n = strlen(word); i < n; i++)
    {
        finish = finish + ((char)(word[i]+1));
        return finish;
    }
}

From what I can gather online, this means that I am not returning a value from my function?

All I want the function to do is add one to the value of the each character inputted into the string array and shift it on the ASCII table and then return the coded message. Is the code I've written here adequate to do that?

4

It means that there is no default return value for your function outside of the for loop. You are assuming that loop will always run, but, what if an empty string ends up getting submitted? The compiler makes no such assumptions, so you need to make sure that there is a return path that will be followed if the return statement inside your loop is never reached.

But, do are you sure you want your return statement inside your loop to begin with? As soon as your program hits that, it will stop executing the loop and return to the main function, performing only a single iteration of the loop. Is that what you want?

You must log in to answer this question.

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