Sol
Sol

Reputation: 11

Why is my method name giving me an error?

My method CalculateFee is giving me an error. It's just a red line under the name "CalculateFee"

I've tried changing public to private and that didn't help, and even int to void even though that was kinda stupid of me...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FineForOverdueBooks
{                   
    class Program   
    {               
        static void Main(string[] args)
        {
            Console.WriteLine("How many books were checked out?");
                int booksCheckedOut = Convert.ToInt32(Console.ReadLine());
            {
                if (booksCheckedOut == 1)
                {
                    Console.WriteLine("How many days is it overdue?");
                }
                else
                    Console.WriteLine("How many days are they overdue?");
            }
            int daysOverdue = Convert.ToInt32(Console.ReadLine());
            int output = CalculateFee(booksCheckedOut, daysOverdue);
            Console.WriteLine("Your late fee is: ", output.ToString("C"));
        }

    public static int **CalculateFee**(int booksCheckedOut, int daysOverdue)
    {
        double libraryFine = .10;
        double additionalFine = .20;
        if (daysOverdue <= 7)
        {
            for (double x = 0; x <= 7; x++)
            {
                libraryFine += .10;
            }
            return Convert.ToInt32(libraryFine);
        }
        if (daysOverdue > 7)
        {
            for (int x =0; x<=daysOverdue; x++)
            {
                additionalFine += .20;
            }
            return Convert.ToInt32(additionalFine);
        }
    }
}
}

I just want my method to be able to execute so I can see if I did my for loops correctly and if I need to change some stuff (which I probably do)

Sorry if this seems like a duplicate I couldn't find anything that directly pertained to my problem.

Upvotes: 0

Views: 297

Answers (1)

TheGeneral
TheGeneral

Reputation: 81583

Because CalculateFee doesn't return in all cases. Actually in this case it just doesn't static analyse all the values to know it can never get there. Consider using an else:

public static int CalculateFee(int booksCheckedOut, int daysOverdue)
{
   double libraryFine = .10;
   double additionalFine = .20;
   if (daysOverdue <= 7)
   {
      for (double x = 0; x <= 7; x++)
      {
         libraryFine += .10;
      }
      return Convert.ToInt32(libraryFine);
   }
   else
   {
      for (int x =0; x<=daysOverdue; x++)
      {
         additionalFine += .20;
      }
      return Convert.ToInt32(additionalFine);
   }

}

Upvotes: 4

Related Questions