blah blah blah is here! blah blah » Close

up1down
link

Hello All,

I am working on a school assignment, a simple console program. I am to ask for input of time in 24 hour time, and convert to minutes(then do some calculations). I am stuck. I was told I could use modulus function to do this, but still can't figure it out.

I do not want to use a military time statement, I want to be able to take in the info. If I can separate the 4 digits, I would be good from there. So user enters 0930, and I can take that in as two pieces, one for 09 hours, and another for 30 minutes.

As you can see, I am in over my head. Can anyone help?

Thanks

last answered one year ago

1 answers

link

I can't see what the modulus function has got to do it with either.

However, try this code instead:

string input = Console.ReadLine(); // 0930 say
int hours = int.Parse(input.Substring(0,2));
int minutes = int.Parse(input.Substring(2,2));
minutes = hours * 60 + minutes;
Console.WriteLine(minutes); // 570

Ideally, you also need to add some error checking code but, hopefully, that will get you started.

mgh24
18

Thank you! That worked beautifully. It took me a minute to figure out that it was necessary to type 0930, and not only 930. I will now play with that substring to see if I can understand it better. Thanks for the time.

Feedback