-3

I don't want the else statement to return a value, but just run the method again. However, I get compile time error

'Program.Coefficient()': not all code paths return a value.

How do I get rid of this error?

This is the code:

public static double Coefficient()
{
    string string1 = Console.ReadLine();
    string[] stringArray = string1.Split('^');

    double[] doubleArray = new double[stringArray.Length];

    for (int i = 0; i < stringArray.Length; i++)
    {
        doubleArray[i] = Double.Parse(stringArray[i]);
    }

    if (doubleArray.Length == 2)
    {
        double coefficient = Math.Pow(doubleArray[0], doubleArray[1]);
        return coefficient;
    }

    else if (doubleArray.Length == 1)
    {
        double coefficient = doubleArray[0];
        return coefficient;
    }
    else
    {
        Console.WriteLine("Please follow the specified input form (a^b).");
        Console.ReadKey();
        Coefficient();
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Anders
  • 23
  • 4

3 Answers3

2

The error means that at least one flow possibility does not return a value, which is the last 'else' in your case.

The last line should be then:

return Coefficient();
Metheny
  • 1,112
  • 1
  • 11
  • 23
  • Will this just run the method again? – Anders Apr 26 '19 at 11:43
  • Recursive call and stackoverflowexception after enough time? – Risto M Apr 26 '19 at 11:44
  • @Anders: Yes, and return the value of that run (this is a recursive call). – Metheny Apr 26 '19 at 11:44
  • @Metheny I'm new to programming so sorry for the many questions, but how does it return a value of the run when there is nothing to return? I just want it to run the method again until a value is returned through the 'if' or 'else if' statement. – Anders Apr 26 '19 at 11:48
  • The original method execution will call the same method (this is called a recursive call). In the second (inner, recursive) execution, in case the user enters a different value, that the condition will allow the 'if' to be evaluated as 'true'. and a return value will be returned to the original method execution which will in turn return the value. – Metheny Apr 26 '19 at 11:53
2

As your function returns value, that means from each if..else block you need to return double value.

Here you are not returning any value from else block. You need to return double value from else block

        else
        {
            Console.WriteLine("Please follow the specified input form (a^b).");
            Console.ReadKey();
            return Coefficient();  // This will call recursively  same function. for recursion use return Coefficient() ;
             //return 0; //If you don't want recursion, then comment above line and return 0

        }

I would prefer to refactor your code to minimize code present in Coefficient() method. something like ,

   public static double Coefficient()
    {
        while (true)
        {
            string string1 = Console.ReadLine();
            string[] stringArray = string1.Split('^');
            double[] doubleArray = Array.ConvertAll(stringArray, double.Parse);


            if (doubleArray.Length == 2)
            {
                double coefficient = Math.Pow(doubleArray[0], doubleArray[1]);
                return coefficient;
            }

            else if (doubleArray.Length == 1)
            {
                return doubleArray[0];
            }
          Console.WriteLine("Please follow the specified input form (a^b).");
        }

    }
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
1

I suggest to redesign the routine (I can't see any need in recursion). You can implement a loop in order to keep asking until user inputs (Console.ReadLine()) valid value:

public static double Coefficient() {
  while (true) {
    string input = Console.ReadLine();

    string[] items = input.Split('^');

    if (items.Length == 1) {
      if (double.TryParse(items[0], out double A))
        return A; // One valid value 
    }
    else if (items.Length == 2) {
      if (double.TryParse(items[0], out double A) && 
          double.TryParse(items[1], out double B))
        return Math.Pow(A, B); // Two valid values
    } 

    // Neither one valid value, nor two valid values pattern 
    Console.WriteLine("Please follow the specified input form (a^b)."); 
    // No need in "Console.ReadKey();" - the routine will stop on Console.ReadLine()
  }          
} 

Be careful with Double.Parse since it throws exception on invalid string (e.g. if user inputs "bla-bla-bla"); use Double.TryParse instead.

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215