Thread: Problem with time converting program.

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    35

    Question 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';
    Thank you for your help in advance.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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";

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    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);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. Problem with time program
    By rushhour in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2009, 08:39 AM
  3. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  4. Pointer problem... i think
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 11-30-2005, 03:45 PM
  5. Replies: 2
    Last Post: 04-25-2005, 11:59 AM