I am having difficulty getting past creating the first function of my code, line 61. I have tried many different variations and I suspect that I am just not understanding a basic concept of the function itself. The error message is "non-void function does not return a value in all control paths".

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

int count_letters(string text);
int count_words(string text);
int count_sentences(string text);

int main(void)
{
// Get input from user
string text = get_string("Text:  ");

int letters;
int words;
int sentences;

letters = count_letters(text);
words = count_words(text);
sentences = count_sentences(text);

// Calculate L and S
float L = 100.00 * (float)(letters/words);
float S = 100.00 * (float)(sentences/words);

// Coleman-Liau index calculation
int index = round(0.0588 * L) - (0.296 * S) - 15.8;

if (index < 1)
{
}
else if (index > 16)
{
}
else
{
}
}

int count_letters(string text)
// Calculate number of letters
{
int LC = 0;
for (int i = 0, n = strlen(text); i <= n; i++)
{
if (isalpha(text[i]))
{
LC++;
}
printf("Letters: %i\n", LC);
return LC;
}
}

int count_words(string text);
//calculate number of words
{
int LW=1;
for (int i = 0; i < n; i++)
{
if (isspace(text[i]))
{
LW++;
}
return LW;
}
}

int count_sentences(string text)
// Calculate number of sentences
{
int LS=0;
for (int i = 0; i < n; i++)
{
if (ispunct(text[i]))
{
LS++;
}
return LS;
}
}

Any help would be greatly appreciated!
``````

First, understand that compilers are dumb but thorough. Let's take a close look at the function:

``````int count_letters(string text)
// Calculate number of letters
{
int LC = 0;
for (int i =0 , n =strlen(text); i <=n; i++ )
{
if (isalpha(text[i]))
{
LC++;
}
printf("Letters: %i\n", LC);
return LC;
}
}
``````

The compiler is going to check if there's any possible way for there to be a path through the function that doesn't return a value, and it found one. The compiler is looking at the case where the for loop doesn't execute. Since compilers are dumb, it can't see that there's no logical way for this to happen (unless n is negative).

So, the compiler looks at what happens if the for loop never executes. In checking the rest of the code, it doesn't see any more return statements and issues an error.

The fix for this is to add a catchall return statement that will return an appropriate value if something goes wrong. In this case, `return -1;` might be a good choice. It would signal that something was wrong by returning a negative number.

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