Thêm số 0 vào trước 1 số

Em có đoạn code sau:

#include<iostream>
int main(){
   int Gio, Phut, Giay;
   Gio=1;
   Phut=20;
   Giay=2;
   std::cout << Gio << ":" << Phut << ":" << Giay;
   return 0;
}

Sau khi chạy:

1:20:2

Giờ làm sao để sau khi chạy nó se hiện số 0 trước Giờ & Giây vậy mấy anh chị? Như dưới:

01:20:02
#include <iostream>
#include <iomanip>
int main() {
   int Gio, Phut, Giay;
   Gio = 1;
   Phut = 20;
   Giay = 2;
   std::cout << std::setw(2) << std::setfill('0') << Gio << ':'
             << std::setw(2) << std::setfill('0') << Phut << ':'
             << std::setw(2) << std::setfill('0') << Giay;
   return 0;
}
5 Likes
int gio = 3, phut = 8, giay = 4;
	cout << setfill('0') << setw(2) << gio
		<< ':' << setw(2) << phut
		<< ':' << setw(2) << giay << endl;
3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?