Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ ****************************************************************************

ID: 3674770 • Letter: C

Question

C++

******************************************************************************************************************************

Write a program to simulate a clock.

Your program should run for approximately 10 seconds.

Use the following main() for your program.

Your printTimeNow() function must make use of the tm struct.

******************************************************************************************************************************
int main()
{
    for (int i = 0; i < 10; i++)
    {
        system("cls"); // Use cls on Windows or clear on Linux to clear the screen
        printTimeNow();
        delay();
    }
}

******************************************************************************************************************************
If you are using Xcode for this exercise, you might want to use another compiler, so you can effectively clear the screen.

Use this delay() function to simulate a delay of one second

void delay()
{
    for (int i = 0; i < 600000000; i++); // Vary the size of this loop to approximate one second  
}


******************************************************************************************************************************
Your output should look like this, displaying the current time,

and changing every second as the program runs.

The current time is 6:02:20 AM

Explanation / Answer

#include <iostream>
#include<stdlib.h>
using namespace std ;

void delay()
{
for (int i = 0; i < 600000000; i++); // Vary the size of this loop to approximate one second
}

void printTimeNow(){
   time_t rawtime;
   struct tm * timeinfo;
   char buffer [80];

   time (&rawtime);
   timeinfo = localtime (&rawtime);

   strftime (buffer,80,"The current time is %r",timeinfo);
   cout<<buffer<<endl;
   }

int main()
{
for (int i = 0; i < 10; i++)
{
system("clear"); // Use cls on Windows or clear on Linux to clear the screen
printTimeNow();
delay();
}
}

/*

Output:

The current time is 10:33:53 PM

*/