Write a C++ program that will create a digital clock. The clock should print out
ID: 3802390 • Letter: W
Question
Write a C++ program that will create a digital clock. The clock should print out a heading for hours, min and sec and should appear in the middle of the screen. Arrange the timing of the clock so that the hours, min and sec track as close as possible to a real clock. The clock will work on classical time with distinction of Am and PM. What this means is that the clock displays from 0 hours to 12 noon as AM and then converts to 1 to 12 PM hour time. So the output would look like
HOURS MIN SEC
2 24 47 PM
The program must contain a "while" loop, a "for" loop and "do while" loop. Hint use the "cls" comand , the #include<string> command and use of "if" statements to decide between Am and PM.
You will need to use the command system("cls") to clear the screen.
Explanation / Answer
#include<stdio.h>
#include <stdlib.h>
#include<windows.h>
#include<time.h>
int main()
{
struct tm *tme;
time_t p;
while(1)
{
p = time(NULL);
tme = localtime(&p);
printf("%d:%0d:%d ",tme->tm_hour,tme->tm_min,tme->tm_sec);
if(tme->tm_hour > 11 && tme->tm_hour <=23) printf("PM"); else printf("AM");
Sleep(1000);
system ("cls");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.