By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,880 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,880 IT Pros & Developers. It's quick & easy.

Error - Not All Code Paths Return a Value

P: n/a
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;
}
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I doubt you want the "return ave;" inside the for() loop. And even if
you do, the compiler is telling you that if number is less than 0 to start,
calcAve will not return a value.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"OutdoorGuy" <Ou********@fishing.com> wrote in message
news:#H**************@tk2msftngp13.phx.gbl...
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;
}
}
}

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2

P: n/a
count = 0.

The program never enters the for loop.

So return is never executed.
OutdoorGuy wrote:
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;
}
}
}

*** Sent via Developersdex http://www.developersdex.com ***

--
Texeme Textcasting Technology
http://texeme.com
Nov 17 '05 #3

P: n/a

"OutdoorGuy" wrote:
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?

Nov 17 '05 #4

P: n/a

Thanks. Putting the following two statements outside of the "For" loop
resolved the problem:

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

Thanks again.
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.