Psychobagger Psychobagger - 11 months ago 76
C Question

Prompt user, wait a few seconds, then end program?

Simple project using a switch statement. I have 4 choices, and 1-3 work great. The 4th choice in the switch statement (case 4, duh) has to prompt the user with "thank you!", then wait for a few seconds so they can read it, then have the program end. I got the "thank you!" part down xD.

I just don't know how to end the program automatically. I tried the exit(0) function and no luck. Also keep in mind that I need to have the message show for a few seconds before it closes.

Here's the code:

Answer Source

You can make your own portable delay function by using the standard clock() function.

#include <stdio.h>
#include <time.h>

void sleeper(unsigned seconds)
    clock_t start, period, elapsed;
    period = seconds * CLOCKS_PER_SEC;
    start = clock();
    do {
        elapsed = clock() - start;
    } while(elapsed < period);

int main(void) {
    printf("Hello, World!\n");
    return 0;