Reputation: 3
I've got 2 problems.
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
.
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
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