Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello! I try to create methods. So i decided to a dice program.

What is done is that a value between 1 - 6 is randomed three times and added to a result.
I use a for loop but i get this weird error(Will quote in a sec)


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static double KastaTärning(double AntalKast)
        {
            for(int x = 0; x==AntalKast;x++)
            {
            Random randomerare = new Random();
            int TarningsResultat = randomerare.Next(1, 6);
            int Resultatet = TarningsResultat;
            Resultatet = TarningsResultat + Resultatet;
            return Resultatet;
            }

        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hur många kast? ");
            int AntalKast = int.Parse(Console.ReadLine());
            Console.WriteLine("Ditt resultat är: " + KastaTärning(AntalKast));
        }
    }
}


What am I doing wrong?


First error is:

VB
Error   1   'ConsoleApplication1.Program.KastaTärning(int)': not all code paths return a value.     10  23  ConsoleApplication1

This marks the KastaTärning method with red underline.

and
MSIL
Warning 2   Unreachable code detected   C:\Users\Weidrup\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs    12  41  ConsoleApplication1


This is marks the "x++" part of for loop.
Posted
Updated 4-May-11 11:57am
v4
Comments
yesotaso 4-May-11 17:49pm    
You made my day dear friend :)
Member 7896310 4-May-11 19:33pm    
How?

When you see this error, it's telling you that there's a return value expected, but the construction of your code means that there are paths that could be reached that don't have a return condition. In the case of the loop, it's because you have the return inside the for loop. What happens if you pass -1 into this method? Well, you'll never enter the loop so the return won't be reached.

As you are attempting to add multiple values in a loop, you should really move the return value outside the for loop. This changes your method to have
C#
  for (....)
  {
  ....
  }
  return Resultaset;
}
 
Share this answer
 
Comments
Member 7896310 4-May-11 17:20pm    
Problem with this is i tried putting it outside the loop, and i get this error:

Error 1 The name 'Resultatet' does not exist in the current context C:\Users\Weidrup\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 19 20 ConsoleApplication1



Also, what did you mean by 'pass -1"?

Thanks for replies
Pete O'Hanlon 4-May-11 17:24pm    
You need to move the definition of Resultaset outside the loop, as in int TarningsResultat = 0;

With pass -1, I mean what happens if your set AntalKast to -1?
Member 7896310 4-May-11 17:37pm    
Alright, It now looks like this:

static int KastaTärning(int AntalKast)
{
int Resultatet = 0;
int Tarningsresultat = 0;
for(int x = 0; x==AntalKast;x++)
{
Random randomerare = new Random();
TarningsResultat = randomerare.Next(1, 6);
Resultatet = TarningsResultat + Resultatet;
}
return Resultatet;
}


Now it gave no errors and i was able to lauch the application. However the returned value is 0...
And if i remove the 0 of the Resultatet in the beginning i get tihs error:

Error 1 Use of unassigned local variable 'Resultatet' C:\Users\Weidrup\documents\visual studio 2010\Projects\Kasta Tärning\Kasta Tärning\Program.cs 18 45 Kasta Tärning

Pete O'Hanlon 4-May-11 17:41pm    
You get this error because your for condition is wrong. Add the int Resultatet = 0; back to the start of the method and change the loop to look like:

for (int x = 0; x < AntalKast; x++)
Member 7896310 4-May-11 17:59pm    
Cheers =) That did the trick =)
In the KastaTärning method, you don't have a return statement. That's the issue.

In your for loop, every time you evaluate something, you are returning it. What's the point in doing that? You are evaluating something 'AntalKast' times in your method, but every time it will return for i = 0. This is not the core issue though. Your return statement should be the last in your method (if it's return type is not void).

The error you are getting necessarily means not every method which should return something returns something.

A small sample

public int method(int i)
{
   int x = 0;
   for (int j = 0; j < i; j++)
   {
     x = x + j;
     // return x; // there is no point in doing this, check your logic
   }
   return x; // You don't do this at all, that's the issue
}


Hope this helps!
 
Share this answer
 
Comments
Member 7896310 4-May-11 17:24pm    
Didn't quite understand what you meant.
The thing it is this; what the KastaTärning method is randomize a number between 1-6, put that into a variable, then do it again, and ADD that to the variable aswell, then return the sum into a Console.Write.

However, i tried your method:

static int KastaTärning(int AntalKast)
{
int Resultatet;
for(int x = 0; x==AntalKast;x++)
{
Random randomerare = new Random();
int TarningsResultat = randomerare.Next(1, 6);
Resultatet = TarningsResultat;
Resultatet = TarningsResultat + Resultatet;
}
return Resultatet;
}

Gave this error:;


Error 1 Use of unassigned local variable 'Resultatet' C:\Users\Weidrup\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 20 20 ConsoleApplication1
Karthik. A 4-May-11 17:28pm    
Try this:

static int KastaTärning(int AntalKast)
{
int Resultatet = 0;
for(int x = 0; x<=AntalKast;x++)
{
Random randomerare = new Random();
int TarningsResultat = randomerare.Next(1, 6);
Resultatet += TarningsResultat;
}
return Resultatet;
}
Member 7896310 4-May-11 17:39pm    
thank you! it works now! I understood now why the Resultatet could be outside, and the Tarningsresultat had to be inside =)

But i did not change "Resultatet += Tarningsreulstat;" what does the "+=" mean?

DaveyM69 4-May-11 18:21pm    
+= adds the right hand side to the left hand side so
Resultatet += Tarningsreulstat
is the same as:
Resultatet = Resultatet + Tarningsreulstat;
Karthik. A 4-May-11 17:30pm    
Why do you use x==AntalKast? any particular reason?
There are several things that need correcting in your code.

You are using doubles where ints will do.
You are testing if x == AntalKast. Unless AntalKast is 0, this will never be true so your loop will never execute.
You are generating the new Random inside the loop. This will normally result in the same numbers being generated - move it outside of the loop.
All paths need to return a value, if the loop doesn't execute then there is no return. Move the definition of the return value before the loop and the actual return after.
This seems very strange - are you sure this is what you meant?
C#
Resultatet = TarningsResultat;
Resultatet = TarningsResultat + Resultatet;

It's normal (but not required) to have method names start with an uppercase character but parameters and fields start with a lowercase character.

The fix to these results in this:
C#
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static int KastaTärning(int antalKast)
        {
            Console.Clear();
            int resultatet = 0;
            Random randomerare = new Random();
            for (int x = 0; x < antalKast; x++)
            {
                int tarningsResultat = randomerare.Next(1, 6);
                Console.Write("Dice: {0} ", tarningsResultat);
                resultatet += tarningsResultat;
            }
            Console.WriteLine();
            return resultatet;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hur många kast? ");
            int antalKast = int.Parse(Console.ReadLine());
            Console.WriteLine("Ditt resultat är: " + KastaTärning(antalKast));
            Console.ReadKey();
        }
    }
}
 
Share this answer
 
v2
Comments
DaveyM69 4-May-11 18:19pm    
By the way, int.Parse can be dangerous. Run the app and enter a normal letter...
Have a look at int.TryParse here: http://msdn.microsoft.com/en-us/library/f02979c7(v=VS.80).aspx
Member 7896310 4-May-11 18:57pm    
Cheers mate! Will use this from now on!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900