• 0

C++ convert minutes to standard time


Question

15 answers to this question

Recommended Posts

  • 0

Divide by 60, there's a start :p

int mins = 720;
int hours = mins / 60;
int minutes = mins % 60;
cout << hours << ":" << minutes << endl;

Link to comment
Share on other sites

  • 0

I would devide by 60, set the result as your hour and the remainder as your minutes. Make sure to use doubles, or devide by 60.0. If you use double, you need to typecast your hour as (int). Otherwise, you'll get something like 2.0 hours. Then just use a simple print statement (if u want to print) to output ur quotient and remainder separated by the a colon ":"

Hope this helps.

EDIT

For the remainder just use the modulus operator %

There's no need for decimal remainders or what not.

Link to comment
Share on other sites

  • 0
cout << hour/60.0 << ":" << hour%60.0

That won't work because you're using doubles. You can't use mod with doubles :p And if you divide by a double, then it will evaluate to a double, which he doesn't want. You'd have to do this:

cout &lt;&lt; hour / 60 &lt;&lt; ":" &lt;&lt; hour % 60;

Link to comment
Share on other sites

  • 0

Whoops, not sure why I stuck .0 on there :wacko:

Guess I saw the .0's in the post before that and my mind somehow stuck them on.

I mean...errr...I don't know what you're talking about :whistle:

Edited by kjordan2001
Link to comment
Share on other sites

  • 0

hi. I tried the

hours = theTime / 60;
minutes = theTime % 60;
cout &lt;&lt; hours &lt;&lt; ":" &lt;&lt; minutes &lt;&lt; endl;

for 1149 mins it shows the standard time as 19:9.. how do I make this 7:09 instead?

Edited by saiz66
Link to comment
Share on other sites

  • 0
hi.  I tried the

hours = theTime / 60;
minutes = theTime % 60;
cout &lt;&lt; hours &lt;&lt; ":" &lt;&lt; minutes &lt;&lt; endl;

It works good except I get 12:0, is there a way to make it 12:00?

cout << (mins < 10 ? "0" : "") << mins << endl;

Subtract 12 if the hour is greater than 12.

Link to comment
Share on other sites

  • 0
thanks for all the help guys but I edited my last post.

"for 1149 mins it shows the standard time as 19:9.. how do I make this 7:09 instead? "

See the last 2 posts, either should work. :whistle:

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.