Question about a program I wrote?

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 22
Reputation: Questions??? is an unknown quantity at this point 
Solved Threads: 0
Questions??? Questions??? is offline Offline
Newbie Poster

Question about a program I wrote?

 
0
  #1
Nov 22nd, 2007
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;
}
}
}




}
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: Question about a program I wrote?

 
0
  #2
Nov 22nd, 2007
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 22
Reputation: Questions??? is an unknown quantity at this point 
Solved Threads: 0
Questions??? Questions??? is offline Offline
Newbie Poster

Re: Question about a program I wrote?

 
0
  #3
Nov 22nd, 2007
Thankyou for the help. I was able to resolve the issue with your help.



Originally Posted by mariocatch View Post
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:
  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC