user3371581
user3371581

Reputation: 3

How to get the timestamp when the user gives an input in C

I've got 2 problems.

  1. How can I make a loop that will keep getting input from user unless he enters a specific input — in my case it's exit.

  2. I need to write the timestamp of the input in a file but I am not that familiar with C.

I need something to loop like this

        if (strcmp(userInput, quit) == 0 ) {
            exit(1);
        } else {
            printf("IS IT IN?!");
        }

Upvotes: 0

Views: 222

Answers (1)

R Sahu
R Sahu

Reputation: 206567

Here's a function you can use to print the current time in the format "HH:MM:SS AM/PM".

void printCurrentTime()
{
   time_t rawtime;
   struct tm* timeinfo;
   char buffer[100];

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

   strftime (buffer, 100, "%I:%M:%S %p", timeinfo);
   printf("Current time: %s.\n", buffer);
}

The looping can be accomplished using:

while (1)
{
    if ( NULL == fgets (useInput, maximumLineLength, stdin))
    {
       exit(1);
    }
    if ( strcmp(useInput, quit) == 0 )
    {
       exit(1);
    }
    printCurrentTime();
}

Upvotes: 1

Related Questions