Having just a few problem with bring the difference of time times back into this form HH: MM:SS. I am required to you % , and fill the remaining space with 0's. Some of my code is just previous rambling that I turned into comments. Any ideas would be of great help.

#include <iostream>
#include <iomanip>

int main()
{
    int hrs1 = 0;
    int min1 = 0;
    int sec1 = 0; 
    int hrs2 = 0; 
    int min2 = 0; 
    int sec2 = 0; 
    int time1 = 0; 
    int time2 = 0; 
    int x = 0;

    std::cout << "enter a first time"; 
    std::cin>> time1;
    std::cout << "enter a second time";
    std::cin>> time2;

    hrs1 = time1 / 10000 * 60 * 60;
    min1 = time1 / 10000 % 100 * 60; 
    sec1 = time1 % 100;

    hrs2 = time2 / 10000 * 60 * 60; 
    min2 = time2 / 10000 % 100 * 60; 
    sec2 = time2 % 100; 

    x = (hrs1 + min1 + sec1) - (hrs2 + min2 + sec2); 


    //hrs = time2 / 10000 * 60 * 60; 
    //min = time2 / 10000 % 100 * 60; 
    //sec = time2 % 100; 
    //time1 = time1 / 10000 * 60 * 60 + time1 / 10000 % 100 * 60 + time1 % 100;
    //time2 = time2 / 10000 * 60 * 60 + time2 / 10000 % 100 * 60 + time2 % 100;
    //timeRemaining = time1 - time2 ;



    std::cout << std::setfill('0') << x % 100000 /60 << ":" << x % 1000 / 10 << ":" << x % 60;

Recommended Answers

All 3 Replies

Your problem statement is not very clear. In what form is the time entered by the user? Total seconds, 24-hour time, StarDate?

How does division by 10,000 come into play?

the time is entered in 24 hour form ex. 050700 would be 05:07:00 am. I was playing around with the division by 10000. I am new to C++ and I wasn't sure if it would work or not.

OK. I don't usually see or do this type problem with seconds included.

You're on a right track, but perhaps doing too much in parts. Consider the input time 082517 (8:25:17am)

Dividing by 10000 you get result 8, the hour. No multipying by 60's needed (unless you're thrn trying to find how many seconds that number of hours is.)

Store the result of time % 10000 and you have the remaining minutes&seconds.
Divide that by 100 to find the minutes (25). Mod by 100 to find remaining seconds (17). You're done.

When it comes to displaying the time, you have options on how to display leading zeros when needed. I'll leave you to work on that for a while.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.