| | |
Question about a program I wrote?
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 22
Reputation:
Solved Threads: 0
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;
}
}
}
}
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;
}
}
}
}
•
•
Join Date: Apr 2007
Posts: 103
Reputation:
Solved Threads: 17
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:
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.
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)
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.
Last edited by mariocatch; Nov 22nd, 2007 at 1:43 am.
•
•
Join Date: Nov 2007
Posts: 22
Reputation:
Solved Threads: 0
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:
C# Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- What's the HARDEST program you've written? (Computer Science)
- currency interest program need help coding it further (C++)
- about c program.. (C)
- Java 5 with Mac OS X (Java)
- Factorial program (Java)
- *Pointer program problem. (C++)
- about:blank hijack (Viruses, Spyware and other Nasties)
Other Threads in the C# Forum
- Previous Thread: Read certain # of lines
- Next Thread: Small problem face me
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom customactiondata database datagrid datagridview dataset date/time datetime datetimepicker degrees development dll draganddrop drawing encryption enum event excel file filename files form format forms function gdi+ gis gtk hash httpwebrequest image index input install java label list listbox mandelbrot math mouseclick mysql operator outlook2003 path photoshop picturebox pixelinversion pixelminversion post programming radians regex remote remoting richtextbox server sleep socket sql statistics stream string table tables tcp text textbox thread time timer tutorial update usercontrol usercontrols validation visualstudio webbrowser webcam wia windows winforms wpf xml





