Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8818483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:07:02+00:00 2026-06-14T05:07:02+00:00

So I have been tackling this problem for a while now, and with the

  • 0

So I have been tackling this problem for a while now, and with the help of members, I am almost finished. My last question for this is above.

I need to format the cout time to 01:01:01 if necessary, however, my output is 1:1:1

The problem is here:

std::ostream& operator<< (ostream& os, const MyTime& m)
{
    os << setfill('0') << m.hours << ":" << setfill ('0') << m.minutes << ":" << setfill ('0') << m.seconds;
    return os;
}

This is the entire code.

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>


using namespace std;
struct MyTime { int hours, minutes, seconds; };
MyTime DetermineElapsedTime(const MyTime *t1, const MyTime *t2);

const int hourSeconds = 3600;
const int minSeconds = 60;
const int dayHours = 24;
const char zero = 0;

MyTime DetermineElapsedTime(const MyTime *t1, const MyTime *t2)
{
    long hourDiff = ((t2->hours * hourSeconds) - (t1->hours * hourSeconds));
    int timeHour = hourDiff / hourSeconds;
    long minDiff = ((t2->minutes * minSeconds) - (t1->minutes * minSeconds));
    int timeMin = minDiff / minSeconds;
    int timeSec = (t2->seconds - t1 -> seconds);
    MyTime time;
    time.hours = timeHour;
    time.minutes = timeMin;
    time.seconds = timeSec;
    return time;
}

std::ostream& operator<< (ostream& os, const MyTime& m)
{
    os << setfill('0') << m.hours << ":" << setfill ('0') << m.minutes << ":" << setfill ('0') << m.seconds;
    return os;
}

int main(void)
{
    char delim1, delim2;
    MyTime tm, tm2;
    cout << "Input two formats for the time. Separate each with a space. Ex: hr:min:sec\n";
    cin >> tm.hours >> delim1 >> tm.minutes >> delim2 >> tm.seconds;
    cin >> tm2.hours >> delim1 >> tm2.minutes >> delim2 >> tm2.seconds;

    if (tm2.hours <= tm.hours && tm2.minutes <= tm.minutes && tm2.seconds <= tm.seconds)
        {
            tm2.hours += dayHours;
        }
    cout << DetermineElapsedTime(&tm, &tm2);


    return 0;

}

Any insight to solve this issue will be greatly appreciated. If you need any more information please ask.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:07:04+00:00Added an answer on June 14, 2026 at 5:07 am

    You need to also call setw to let the stream know it has to pad the output. Keep in mind that in contrast to setfill, setw only applies to the next piece of output.

    std::ostream& operator<< (ostream& os, const MyTime& m)
    {
        return os << setfill('0') << setw(2) << m.hours << ":"
                  << setw(2) << m.minutes << ":"
                  << setw(2) << m.seconds;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been tackling this for a while, and I need some help designing my
I have been tackling this problem for some time. I get the image from
Have been working on this question for a couple hours and have come close
I have been trying to find a good solution to this problem for a
Have been trying to fix this problem I have here. Basically I have a
I have been working with SQL Server as a Developer a while. One thing
have been able to output images from BLOB, however I am now wanting to
have been trying couple of hours now to make my iphone app universal. The
have been working and wearing out my fingers doing google searches with this one:
The following problem has been puzzling me for a couple of days (nb: this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.