So I was looking at this problem , and in monodevelop (and Visual Studio). But for somereason I keep getting unreachable code or error in Visual Studio, I retyped and relooked over my code but I cant seem to find what the mistake is that I can run my code.
I think the problem has a typo or done wrong....

using System;

namespace MilesPerGallon
{
	class MainClass
	{
		
		public static double CalculateMilesPerGallon
						(int milesTraveled, double gallonsUsed)
		{ 
			 return milesTraveled / gallonsUsed;
			
			Console.WriteLine("Miles Per Gallon = {0:N2}", CalculateMilesPerGallon(289, 12.2));
			Console.Read();
		}
	}
}

The code on line 13 and 14 can never be executed because of the return in line 11.
These lines should perhaps be in a void Main() method.

Edited 5 Years Ago by nick.crane: n/a

Two things:
1) return ends the execution of the current method and if that method has a return type, tells it what value to return.

2) Both of your parameters are integers, so the division in your return statement is integer division. It is then converted to a double (the return type). This will result in values you will classify as wrong. For example, if you sent in 5 and 4, the result would be 1.0, not 1.25 as you would expect. Either require at least one of the parameters to be a double or cast them into doubles before performing your math.

Two things:
1) return ends the execution of the current method and if that method has a return type, tells it what value to return.

2) Both of your parameters are integers, so the division in your return statement is integer division. It is then converted to a double (the return type). This will result in values you will classify as wrong. For example, if you sent in 5 and 4, the result would be 1.0, not 1.25 as you would expect. Either require at least one of the parameters to be a double or cast them into doubles before performing your math.

It looks like one of the parameters is a double...

hmm ok I will look at this problem again thanks guys !

P.S this is unrelated but on this site you cant add reputation to more than one person? Its happened twice that I wanted to giv rep to all you guys especially @Momerath

Edited 5 Years Ago by techlawsam: n/a

I'm still new to C# myself, but i came across this "unreachable code" error too. But putting the code before the return statement fixed it for me..

public static double CalculateMilesPerGallon
						(int milesTraveled, double gallonsUsed)
		{ 
			 
 
			Console.WriteLine("Miles Per Gallon = {0:N2}", CalculateMilesPerGallon(289, 12.2));
			Console.Read();

return milesTraveled / gallonsUsed;
		}

Would that work?.. sorry not at home, so no VS...

What about assigning a int which gets the value, the return it at the end?

public static double CalculateMilesPerGallon
						(int milesTraveled, int gallonsUsed)
		{ 
			 
 int cMPG = milesTraveled / gallonsUsed;
			Console.WriteLine("Miles Per Gallon = {0:N2}", CalculateMilesPerGallon(289, 12.2));
			Console.Read();

return cMPG ;
		}

Hope this helps..

@walid86 The way you coded it there is a problem with this line:

Console.WriteLine("Miles Per Gallon = {0:N2}", CalculateMilesPerGallon(289, 12.2));

Since CalculateMilesPerGallon is called it will cause an infinite recursive loop.

As I said before this line (and Console.Read(); ) needs to be in a separate method; more like this.

using System;

namespace MilesPerGallon
{
	class MainClass
	{
		
		public static double CalculateMilesPerGallon(int milesTraveled, double gallonsUsed)
		{ 
			 return milesTraveled / gallonsUsed;
		}

		public static void Main(string[] args)
		{ 
			Console.WriteLine("Miles Per Gallon = {0:N2}", CalculateMilesPerGallon(289, 12.2));
			Console.Read();
		}


	}
}

@nick.crane thanks, i just thought i'd put my two cents in about code after a return statement. then copy/pasted the other code...

your solving everything man!.. good work!. i want your code knowledge!
....my project would've been finished by now!..

This question has already been answered. Start a new discussion instead.