Error - Not All Code Paths Return a Value

  • Thread starter Thread starter OutdoorGuy
  • Start date Start date
O

OutdoorGuy

Greetings,

I am attempting to compile the code below, but I am receiving an error
message when I do so. The error message is: "CSO161:
'Forloop.CalcAvg(int)': Not all code paths return a value". Any idea as
to what I'm doing wrong? I'm sure it's something simple.

Thanks in advance!

public class ForLoop
{
public static void Main()
{
String strNumber;
int number;
double result;

Console.Out.WriteLine("Please enter in a number:");
strNumber = Console.ReadLine();
number = Convert.ToInt32(strNumber);
result = calcAve(number);
}

public static double calcAve(int number)
{
String strValue;
int total, value, count;
double ave;

total = 0;

for(count = 0; count < number; count ++)
{
Console.Out.Write("Enter a value: ");
strValue = Console.ReadLine();
value = Convert.ToInt32(strValue);

total = total + value;
ave = (double)total / count;
return ave;
}
}
}
 
OutdoorGuy said:
Greetings,

I am attempting to compile the code below, but I am receiving an error
message when I do so. The error message is: "CSO161:
'Forloop.CalcAvg(int)': Not all code paths return a value". Any idea
as
to what I'm doing wrong? I'm sure it's something simple.

Thanks in advance!

public class ForLoop
{
public static void Main()
{
String strNumber;
int number;
double result;

Console.Out.WriteLine("Please enter in a number:");
strNumber = Console.ReadLine();
number = Convert.ToInt32(strNumber);
result = calcAve(number);
}

public static double calcAve(int number)
{
String strValue;
int total, value, count;
double ave;

total = 0;

for(count = 0; count < number; count ++)
{
Console.Out.Write("Enter a value: ");
strValue = Console.ReadLine();
value = Convert.ToInt32(strValue);

total = total + value;
ave = (double)total / count;
return ave;
}
}
}

You return inside the for loop. First, say you pass a negative number to
the function. When the 'count' variable at the beginning of the for loop
is initialized to zero and it's compared to 'number,' it will skip the
for loop entirely. The function ends without a return statement after
the for loop, so, as the error message says, not all paths return a
value.

Second, are you sure you want to put the return statement in the for
loop? The loop will only get executed once at most, so I'm thinking
you've got a mistake in your logic. It looks like you want to calculate
the average -after- you've completed the for loop. So take that out of
the loop and place it immediately after and then return the average.

Erm, at another glance, it looks like you've got a division by zero
error as well. Through the first iteration of the loop, count is zero,
and you're using it as a divisor. Maybe you want to use 'number' as the
divisor since you're computing the average?
 
Thanks. Putting the following two statements outside of the "For" loop
resolved the problem:

ave = (double)total / count;
return ave;

Thanks again.
 
Back
Top