Page 1 of 1

I have an assignment that asks me to make a program that displays the time and date in C++. I have it coded but I notice

Posted: Sun May 15, 2022 12:45 pm
by answerhappygod
I have an assignment that asks me to make a program that
displays the time and date in C++. I have it coded but I noticed a
problem yesterday where if it's 7:05 PM for example, it shows 7:5
PM or something like that, the 0 seems to disappear. I will
provide my current code below.
#include <ctime>
#include <chrono>
#include <iostream>
using namespace std;
#include <stdio.h>
#include <time.h>
int main()
{
time_t current_time;
struct tm local_time;
time(&current_time);
localtime_s(&local_time,
&current_time);
string timeDisplay;
int Year = local_time.tm_year + 1900;
int Month = local_time.tm_mon + 1;
int Day = local_time.tm_mday;
int Hour = local_time.tm_hour -
12;
int Min = local_time.tm_min;
int Sec = local_time.tm_sec;
if (Hour >= 12)
timeDisplay = "AM";
else if (Hour < 12)
timeDisplay =
"PM";
cout << Hour << ":"
<< Min << " " << timeDisplay;
cout << "\n";
cout << Month << "/" << Day
<< "/" << Year;
return 0;
}