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

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

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

Post 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;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply