1

I have a code here that would probably just delay 10 seconds before proceeding , but I want to add a feature that the user could wait that delay or press any key to continue, I know it wouldn't be just like delay(10000) || getch(); any way to do it?

#include <stdio.h>
#include <conio.h>
#include <dos.h>

void main(){
    clrscr();
    printf("Press Any Key or Wait for 10 Seconds to Continue");
    delay(10000); 
    //getch();
}
ruakh
  • 175,680
  • 26
  • 273
  • 307
Aesthetic
  • 763
  • 1
  • 14
  • 31
  • 2
    You can do this quite nicely with threads. Do you know much about that? – Bathsheba Oct 24 '13 at 14:24
  • You could take a timestamp and do a busy wait loop that ends when a key was pressed or a new timestamp shows 10s have passed – Leeor Oct 24 '13 at 14:26
  • possible duplicate of [How to deactivate input statement after some time?](http://stackoverflow.com/questions/18289635/how-to-deactivate-input-statement-after-some-time) – alk Oct 24 '13 at 16:48
  • 1
    This answer provides an appraoch for IX'ish (non-windows) systems: http://stackoverflow.com/a/18289845/694576 It takes the opposite approach: Go into blocking read and cancel it after a certain amount of time. – alk Oct 24 '13 at 16:49
  • @alk Sir, I'm learning only C as have been said on my title have been edited by ruakh, I aslo have tried your code in my compiler and because I have no idea C++ yet, I don't know if it's my problem that there's much error starting from the error - the UNISTD.h header couldn't be found, obiously I don't have that header... – Aesthetic Oct 26 '13 at 05:47

2 Answers2

2

It's actually quite easy using alarm:

printf("Press Any Key or Wait for 10 Seconds to Continue");
alarm(10);
getch();
alarm(0);

You might need to set a handler on SIGALRM though.

aragaer
  • 17,238
  • 6
  • 47
  • 49
  • Please see my comment on the OP, for a link to a full example using this approach. – alk Oct 25 '13 at 11:11
1
#include <stdio.h>
#include <conio.h>
#include <dos.h>

void main()
{
    int wait = 10000;

    clrscr();
    printf("Press Any Key or Wait for 10 Seconds to Continue\n");

    while(!kbhit() && wait > 0)
    {
        delay(100);
        wait -= 100;
    }
}
Paul
  • 6,061
  • 6
  • 39
  • 70
  • http://cboard.cprogramming.com/c-programming/63166-kbhit-linux.html has an implementation of `_kbhit` for linix if you need one. – AShelly Oct 24 '13 at 14:39
  • @alk Sir, the term 'portable' is new to me? What the disadvantage if I use this "Not very portable" code? – Aesthetic Oct 26 '13 at 05:56
  • 1
    @Daniel: "*portable*" in this context referrs to the "*portability*" of source code. For details you might like to read here: http://en.wikipedia.org/wiki/Software_portability Here: The `kbhit()` function isn't a standard C function, so it most likely isn't available on each system C is available on. So "*porting*" this code from one platform to another might be difficult to impossible, at least without modifications to the code. So the code isn't (very) "*portable*". – alk Oct 29 '13 at 08:20
  • @alk, thank you, better I have known it earlier, maybe that's why many languages are C-based... – Aesthetic Oct 29 '13 at 11:49