wilfy
wilfy

Reputation: 17

C# Pass user input to method, and output result (from the main)?

fairly new so please forgive the question. I've written a method to take a string, covert it to a number, and return a Factorial of that number. That works fine. Its calling the method and printing the result that is confusing me. Here is the code:

static void Main(string[] args)
{
    Console.WriteLine("Type number to do factorial on..");
    var calc = Fact(Console.ReadLine());
    Console.WriteLine("The answer is " + calc);
}

private static string Fact(string numFact)
{
    var number = 1;
    int factorial = Convert.ToInt32(numFact);
    for (int i = 1; i<= factorial; i++)
    {
        number *= i;
    }
   // Console.WriteLine(number); added to test it works
    return numFact;
}

Can somebody help please? As you'll probably guess from looking, If I input 5, I get 5 returned.

Upvotes: 1

Views: 4549

Answers (2)

P.Brian.Mackey
P.Brian.Mackey

Reputation: 44285

You should dig a bit deeper with your logging. You logged only 1 of 2 critical variables and so you didn't get the answers you needed from the logs. Also, take the Console.Readline() out of the equation. Use a hard coded number. Eventually you can write tests. Checkout Test Driven Development (TDD) which is perfect for this exercise.

Just modifying the logs without TDD can bring out something like this:

static void Main(string[] args)
{
    //Console.WriteLine("Type number to do factorial on..");
    int sampleNumber = 5;
    string sampleNumberAsString = sampleNumber.ToString();
    var calc = Fact(sampleNumberAsString);
    Console.WriteLine("Outside fact the answer is " + calc);
}


private static int Fact(string numFact)
{
    var number = 1;
    int factorial = Convert.ToInt32(numFact);
    for (int i = 1; i <= factorial; i++)
    {
        number *= i;
    }
    Console.WriteLine($"Inside Fact() local variable number is {number}");//Inside Fact() local variable number is 120
    Console.WriteLine($"Inside Fact() numFact parameter is is {numFact}");//Inside Fact() numFact parameter is is 5
    //return numFact;//Clearly I want to return the calculated value (number) not the original value passed in (numFact)
    //I'm calculating a number so let's not worry about what the caller does with the number
    //Change the return type to int.
    return number;
}

Upvotes: 0

Shelby115
Shelby115

Reputation: 2867

static void Main(string[] args)
{
    Console.WriteLine("Type number to do factorial on..");
    var calc = Fact(Console.ReadLine());
    Console.WriteLine("The answer is " + calc);
}

private static int Fact(string numFact)
{
    var number = 1;
    int factorial = Convert.ToInt32(numFact);
    for (int i = 1; i<= factorial; i++)
    {
        number *= i;
    }
   // Console.WriteLine(number); added to test it works
    return number;
}

As the comments mention,

  • You are returning numFact instead of number.
  • You should make it return an int instead of string for a better practice, though this isn't exactly necessary, it just feels more natural since it is a number. Make it a string once you need it as a string, doing so early could cause problems later; however, since this is a very small project the likelihood of that is small so do what you like.

Upvotes: 3

Related Questions