-2

I'm trying to make a program that finds the factors of a number. I made a fairly simple one but it always repeated the same two factors twice i.e. 1 and 2, 2 and 1. So, to fix that I tried to check if the number had been used before but it keeps saying the bool proceed is unassigned.

using System;

namespace FactorableOrNah
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Console.WriteLine ("Enter a whole number to view its factors: ");

            int userInput = int.Parse(Console.ReadLine ());
            int[] antiDoubler = new int[userInput];
            bool proceed;

            Console.Clear();

            for (int i = 1; i != userInput; i++) {

                antiDoubler[i] = userInput / i;

                for(int j = 0; j < userInput; j++) {

                    if (antiDoubler [j] == i)
                        proceed = false;
                    else
                        proceed = true;
                }

                if ((userInput % i) == 0 && i != 1 && proceed == true)
                    Console.WriteLine("{0} and {1}", i, (userInput / i));
            }
        }
    }
}
  • 7
    what happens if userInput is zero – Sneftel Jul 13 '14 at 09:40
  • 3
    @Sneftel: somewhere a reactor casing starts hissing.... – Mitch Wheat Jul 13 '14 at 09:41
  • 4
    Give `proceed` a default value of `false` (`bool proceed = false;`). It is a compiler error to try and use a local variable that may not have been assigned anything. – Phil K Jul 13 '14 at 09:41
  • 2
    don't do stuff like proceed == true. It is just proceed or !proceed with booleans – T_D Jul 13 '14 at 09:56
  • 1
    What have you done to try to understand this error? It is a fairly simple error. Do you understand under which conditions `proceed` would not be assigned? Do you understand why it has to be assigned? – default Jul 13 '14 at 10:11
  • @Default sorry if I didn't make that clear. I really don't understand under which case proceed is unassigned. That probably shouldve been my original question. Could you explain it? – user3833958 Jul 13 '14 at 19:00

2 Answers2

0

Using uninitialized variables in C# is not allowed. The compilation error can be solved by using either:

bool proceed = false;

or

bool proceed = default(bool);

since the default value of bool is false;

However, the algorithm is too complicated and very hard to read. Just for fun. A recursive example.

    static IEnumerable<int> GetFactors(int number)
    {
        return GetFactors(number, number);
    }

    static IEnumerable<int> GetFactors(int number, int check)
    {
        if (check > 0)
        {
            if (number % check == 0)
            {
                yield return check;
            }

            foreach (var f in GetFactors(number, --check))
            {
                yield return f;
            }
        }
    }

UPDATE:

Local variables cannot be left uninitialized, however class members (static members and instance variables), furthermore array elements are initialized automatically by the memory manager, so they are never uniitialized.

Daniel Leiszen
  • 1,827
  • 20
  • 39
  • 1
    This might solve the OPs current algorithm issue, but it doesn't explain his question, i.e. why "bool proceed is unassigned". Could you add an explanation about that to your answer as well? – default Jul 13 '14 at 10:17
  • To be precise, use of unassigned "local" variable is not allowed in C#. – itsbalur Jul 13 '14 at 10:44
  • @Daniel: I was referring to "fields" or "instance variables" as they are called, which assumes a value by default. http://stackoverflow.com/questions/1542824/initialization-of-instance-fields-vs-local-variables – itsbalur Jul 13 '14 at 12:17
0

From the specification:

A variable must be definitely assigned (§5.3) before its value can be obtained. As described in the following sections, variables are either initially assigned or initially unassigned. An initially assigned variable has a well-defined initial value and is always considered definitely assigned. An initially unassigned variable has no initial value. For an initially unassigned variable to be considered definitely assigned at a certain location, an assignment to the variable must occur in every possible execution path leading to that location.

For your case you have an initially unassigned variable. Thus, the variable must be set in every possible execution path. There is one possible execution path to which your variable is not defined - when userInput >= j.

This would happen if userInput is 0. Following your program manually:

  • The first for case will check if i != userInput. Since i = 1 this is true, thus it will continue in the for loop.
  • the second for case will check if j < userInput. Since j = 0 this is false, thus it will skip the for case and never set proceed
  • Now you have arrived to where you check proceed and it was never set. So the compiler tells you that this is not allowed.

To solve your issue, you have to decide whether to:

  • define a default value for proceed, for instance false and set it at declaration, i.e. bool proceed = false;.
  • Rewrite your logic so that you do not need the boolean, for instance like Daniel Leiszen suggests.
Community
  • 1
  • 1
default
  • 11,485
  • 9
  • 66
  • 102