I am a student. I have written the following program for an assignment. The assignment is to seconds into Hours, minutes and seconds. However, I think I have done the program right, but I keep getting the following two errors: ComputeMinutesAndSeconds (int): not all code code paths have a return and on ComputeHoursMinutesAndSeconds(int): not all code paths have a return.

If someone would be willing to offer me a little advice I would appreciate it.

using System;
/*This program is designed to find the amount of hours, minutes and seconds, that are in the number of seconds givenh by the user*/  

public class Lab4
{
    public static void Main()
    {
        int number,
            result1,
            result2;

        InputMethod(out number);
        result1 = ComputeMinutesAndSeconds(number);
        result2 = ComputeHoursMinutesAndSeconds(number);

    }
    public static void InputMethod(out int number)
    {
        string n1;
        Console.WriteLine("Enter any number of seconds and this program will covert your number of seconds to: hours, minutes and seconds");
        n1 = Console.ReadLine();
        number = Convert.ToInt32(n1);

    }
    public static int ComputeMinutesAndSeconds(int num)
    {
        int sec = 1;
        int minutes = sec * 60;
        int ans_Min = 0;
        int ans_Sec = 0;

        while (num < 3600)
        {
            ans_Min = num/minutes;
            ans_Sec = ((ans_Min * minutes) - num) * sec;
            Console.WriteLine("There are {0} minutes and {1} seconds in {2} seconds",ans_Min, ans_Sec, num);
            if (num > 3600)
            {
                return num;
                break;
            }
        }
    }
    public static int ComputeHoursMinutesAndSeconds(int num)
    {
        int sec = 1;
        int hours = sec * 3600;
        int minutes = sec * 60;
        int ans_Hour = 0;
        int ans_Min = 0;
        int ans_Sec = 0;
        int numberX = 0;

        while (num >= 3600)
        {
            ans_Hour = num/hours;
            numberX = ((ans_Hour * hours)- num);
            ans_Min = numberX/minutes;
            Console.WriteLine("There are {0} hours, {1} minutes, and {2} seconds in {3} seconds", ans_Hour, ans_Min, ans_Sec, num);
            ans_Sec = ((ans_Min * minutes)- numberX)* sec;
            if (num < 3600)
            {
                return num;
                break;
            }
        }
    }




}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

For the love of god surround that in code tags...

BTW, not all code paths return a value means:

Your methods do not return a value in EVERY scenario. There are some scenarios that do not return anything.

So taking your ComputeHoursMinutesAndSeconds method.

It ends with:

if (num < 3600)
{
       return num;
       break;
}

So look at this closely... there's actually 2 problems here.
1) You are returning ONLY if the num is < 3600... What if it is greater than or equal to 3600? You don't do anything, no returns, and that's where your compiler errors are from.

2) You are returning, THEN breaking... The way return works is, once it's called, that method is done. The break actually never gets hit. And i'm sure when you compile that, it says as a warning, unreachable code detected on that 'break' statement.

Thankyou for the help. I was able to resolve the issue with your help.


For the love of god surround that in code tags...

BTW, not all code paths return a value means:

Your methods do not return a value in EVERY scenario. There are some scenarios that do not return anything.

So taking your ComputeHoursMinutesAndSeconds method.

It ends with:

if (num < 3600)
{
       return num;
       break;
}

So look at this closely... there's actually 2 problems here.
1) You are returning ONLY if the num is < 3600... What if it is greater than or equal to 3600? You don't do anything, no returns, and that's where your compiler errors are from.

2) You are returning, THEN breaking... The way return works is, once it's called, that method is done. The break actually never gets hit. And i'm sure when you compile that, it says as a warning, unreachable code detected on that 'break' statement.

This question has already been answered. Start a new discussion instead.