473,480 Members | 2,123 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Error - Not All Code Paths Return a Value

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
4 12974
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
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

"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5004
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
5
16206
by: Tony Wright | last post by:
Hi, I am having a problem installing an msi for a web site. The error message I am getting is: "The specified path 'http://mipdev05/features/Fas2' is unavailable. The Internet Information...
6
1584
by: Peter Afonin | last post by:
Hello: I'm trying to convert the connection class that I created in VB.Net for ASP.Net application into C#. I'm doing something incorrectly, because I'm getting the following error when trying...
12
4097
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
3
8626
by: Oberon | last post by:
How do I deal with this? I am getting an error for each get in the Game class (see code below). In the simplified example below I have reduced this to just 3 fields, one which can be NULL. I...
7
4975
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
7
10794
by: siri11 | last post by:
hi everyone!!!! I have written a function that shud return a decimal value.But I'm getting an error in c#.net(windows Application) which is a build error--->not all code paths return a value.Can...
1
1797
by: fretIT | last post by:
Hello, when I write web method using C# in Visual basic 2005, I can't return the string value to the client request. I got such kind of error Not all code paths return a value. Don't know how...
10
6931
by: happyse27 | last post by:
Hi All, I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration...
0
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6743
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4787
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.