Home > Software engineering >  I want my clock program to display the output as 00:00 instead it displays it as 0:0 even though I h
I want my clock program to display the output as 00:00 instead it displays it as 0:0 even though I h

Time:02-20

So I made a sloppy clock program as an assignment. It works fine however I want the output to display each cout as "01:02", instead it displays "1:2", even though I think I have written all the right manipulators. This is my whole program.

#include <thread>
#include <chrono>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdlib.h>
using namespace std;
int main()
{
    cout << fixed << right;
    cout.fill(0);
    for (int m = 0; m < 59; m  )
    {
        for (int s = 0; s < 59; s  )
        {
            this_thread::sleep_for(
                chrono::seconds(1));
            system("cls");
            cout << setw(2) << m << ":";
            cout << setw(2) << s << endl;
        }
    }
}

CodePudding user response:

You also need to do a setfill('0') in addition with setw().

setw() simply sets the minimum width for the formatted output operation. The unused part of the field's width defaults to spaces. setfill() changes that.

CodePudding user response:

You are using the null character to fill (which is not visible).

cout.fill(0);

What you probably meant was to use the ASCII character 0, like this:

cout.fill('0');
  •  Tags:  
  • c
  • Related