1

I have checked a bunch of places with very minimal help to help me figure out a methods way in visual studios to open up the console when running the code to prompt the user from the Console.Writeline("Please enter seconds to convert: "); to input the number of seconds that they are trying to convert, once thats put in it would then print the results of the conversion as "Your result is 00:00:00" . Im still in the process of learning more about c# so i have one slightly basic understanding so far and a basic solution but what i currently have is not that I'm trying to have for this project. My solution so far is :

class Program
{
    static void Main(string[] args)
    {
        TimeSpan t = TimeSpan.FromSeconds(36100556);
        Console.WriteLine(t.Days);
        Console.WriteLine(t.Hours);
        Console.WriteLine(t.Minutes);
        Console.WriteLine(t.Seconds);
        Console.WriteLine(t.ToString());
    }
}
johnk3
  • 13
  • 7
  • Are you trying to do: `Console.WriteLine(DateTime.Now.ToString("hh:mm:ss")`? You can use the built in `ToString` method to actually format a time directly. – Greg Feb 24 '16 at 16:24
  • cant you just use simple time math? `minutes = seconds / 60`, `hour = minutes / 60`, and so on? – Jacobr365 Feb 24 '16 at 16:24
  • 3
    It looks like you need to learn about [`Console.ReadLine`](https://msdn.microsoft.com/en-us/library/system.console.readline(v=vs.110).aspx) and [`TimeSpan.ToString`](https://msdn.microsoft.com/en-us/library/1ecy8h51(v=vs.110).aspx). – James Thorpe Feb 24 '16 at 16:25
  • It seems title is misleading, real problem is to input values and then it's a duplicate of [Read user input from console](http://stackoverflow.com/questions/7280591/read-user-input-from-console) – Sinatr Feb 24 '16 at 16:46

4 Answers4

1

I think you need to follow these steps:

1) Send the message requesting the time with the Console.WriteLine.

2) Use the command Console.ReadLine, this will make the console to wait until the user puts the time that need to convert.

3) Read the value and make the math.

4) Display the value using WriteLine.

5) After display the value, put another Consol.ReadLine in order to avoid the console to hide before the user reads the result.

Hope it helps

Matt
  • 4,656
  • 1
  • 22
  • 32
Ruben Maldonado
  • 227
  • 1
  • 2
  • 8
0

You can use var x = Console.ReadLine() to read input from the console. May have to do some conversions since it will read a string of characters. You can utilize something like Convert.ToDateTime(x) or something to convert to whatever data type you need.

ProgrammingDude
  • 597
  • 1
  • 5
  • 25
0
Console.Write("Enter Number of Seconds: ");
string SecondsStr = Console.ReadLine();

Double Seconds = 0;
if(!Double.TryParse(SecondsStr, out Seconds))
{
    Console.WriteLine("Invalid number entered");
}
else
{
    TimeSpan Time = TimeSpan.FromSeconds(Seconds);    
    Console.WriteLine(string.Format("Days: {0}, Hours: {1}, Minutes: {2}, Seconds: {3}", Time.Days, Time.Hours, Time.Minutes, Time.Seconds));
}
CathalMF
  • 9,705
  • 6
  • 70
  • 106
0
Console.WriteLine("Enter number of seconds:");
double sec = 0;
// Check if user input correct integer
while(!double.TryParse(Console.ReadLine(),out sec))
{
    Console.WriteLine("Your data is invalid. Please input again:");
}
TimeSpan t = TimeSpan.FromSeconds(sec);
Console.WriteLine("Your result is " + t.ToString(@"hh\:mm\:ss"));

Console.ReadLine();
NoName
  • 7,940
  • 13
  • 56
  • 108