944,089 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 860
  • C# RSS
Nov 22nd, 2007
0

Question about a program I wrote?

Expand Post »
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;
}
}
}




}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Questions??? is offline Offline
22 posts
since Nov 2007
Nov 22nd, 2007
0

Re: Question about a program I wrote?

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:
C# Syntax (Toggle Plain Text)
  1. if (num < 3600)
  2. {
  3. return num;
  4. break;
  5. }

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.
Last edited by mariocatch; Nov 22nd, 2007 at 1:43 am.
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
Nov 22nd, 2007
0

Re: Question about a program I wrote?

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



Click to Expand / Collapse  Quote originally posted by mariocatch ...
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:
C# Syntax (Toggle Plain Text)
  1. if (num < 3600)
  2. {
  3. return num;
  4. break;
  5. }

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Questions??? is offline Offline
22 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Read certain # of lines
Next Thread in C# Forum Timeline: Small problem face me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC