# Problem with time converting program.

• 11-24-2009
brian75
Problem with time converting program.
I am working on an excersise from one of my programming books and I need to get the program to take a time in minutes and convert it to hours and minutes.

Example- The user would input a time in minutes, such as, 90 minutes.
The program will then output the time in hours and minutes, such as, 1 hour 30 minutes.

I am completely lost when it comes to this. I was able to get a program to print out the reverse but can't get it to do it like this. The closest thing I can get is a 1.5 print out.

The program I have so far is:

Code:

```#include <iostream> int hour; int min; int total; int main() {         std::cout << "How long did it take you to run the marathon in minutes?  " << '\n';         std::cout << "                    Minute: ";         std::cin >> min;         total = min / 60;         std::cout << '\n' << total << '\n';         return(0); }```
The final print out
Code:

`std::cout << '\n' << total << '\n';`
was something that I just wrote in to see if I was able to get the rest of the program to print what I wanted. The program will be completed with something such as
Code:

`std::cout << '\n' "Your total time took to complete the marathon in hour minute form was << total << '\n';`
• 11-24-2009
Well since you seem to understand how to get the hours, you just need to know how many minutes are left over. There is a "mod" (modulo/modulus) operator that gives you the remainder after a division. So the remainder after dividing 90/60 is 30, which is exactly what you want. So just look up an example of how to use it. The actual operator is "%", and is used just as any other binary operator (+, -, /, etc). Of course there are other ways of doing it, for example repeatedly subtracting 60, until the number is less than 60. So 90 - 60 is 30, so there is one hour, and whatever is left, 30 minutes.

I wouldnt worry about the formatting, as that should be easy, once you have it calculating the correct numbers. I think you should use "endl" or "std::endl" instead of "'\n'", like:
Code:

`cout << "this is first line" << endl << "and now second line";`
• 11-24-2009
brian75
I got it to work. Thank you for the help.

Here is the working code I have

Code:

``` /* Program to convert minutes to hours and minutes */ #include <iostream> int hour; int min; int total; int totalA; int main() {         std::cout << "How long did it take you to run the marathon in minutes?  " << '\n';         std::cout << "                    Minute: ";         std::cin >> min;         total = min / 60;         totalA = min % 60;         std::cout << '\n' << "Your total maration time in a minute hour format is " << total << " hour " << totalA << " minutes." << '\n' << '\n';         return(0); }```