-2

how can i do timer on loop/while that inside i have a scanf ?

if the time is over and the user has not entered any input the loop/while is over! Like a trivia..

while(30 sec)
{
scanf("%d,&try);
}

if the 30 sec end the loop is over and inside try i have -1!!

i can user only a 4 include..

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

tnx!

Dudu Moyal
  • 11
  • 2
  • 2
    There's no standard C solution to this. You need platform specific features. On Unix/Linux, you might do what you want most simply by using `alarm()` to interrupt your `scanf` call. But this is very crude and not generally applicable to most situations, just enough to solve your particular problem. – hyde Dec 13 '16 at 07:12
  • 1
    sry...on Windows! – Dudu Moyal Dec 13 '16 at 07:15
  • 1
    You would have to write your own scanf for that using platform dependent functionality. E.g. you seem to be doing a console program so check out https://msdn.microsoft.com/en-us/library/windows/desktop/ms684199(v=vs.85).aspx – AndersK Dec 13 '16 at 07:16

2 Answers2

3

It is not possible with C standard library only, so those 4 headers you list are not enough. scanf will wait for the user to press enter, and there's no way to get it to interrupt after 30 seconds without using platform specific features or extra libraries (which hide the platform specific features).


As a guideline for generic solution, you need a way to get characters from terminal so that you can also interrupt after a specific time. So you would read character by character and put them to a buffer, until your time limit is reached or until user presses enter (note that this is not possible with for example getchar(), because even though it gives you one character, it will still block until user presses enter, and then give you the entered characters one by one).

Then once you have an entire line (or maybe until space, or until you have read N digits, or whatever you want to do), use sscanf or strtol or whatever to parse the string and get the integer you want.

hyde
  • 60,639
  • 21
  • 115
  • 176
0

With other librarys this would be possible. Look at this question.

Look here for a example with kbhit. You will have some restrictions with it, see the comments for details.

Community
  • 1
  • 1
izlin
  • 2,129
  • 24
  • 30
  • The OP is using Windows, not MS DOS. In fact, nobody uses MS DOS. Stop recommending it. – Lundin Dec 13 '16 at 07:26
  • Well, more generically, you could look at putting the keyboard in *raw-unbuffered* mode and then using `select` with a timeout for a non-blocking implementation. There are a couple of standard `kbhit` type functions written using the C standard lib headers. – David C. Rankin Dec 13 '16 at 07:34
  • @Lundin I think Conio is quite ok for Windows terminal. Just because it originates in MS-DOS isn't a reason to avoid it. There might be other reasons (like, it seems to require polling with very short sleep between, not sure), but MS-DOS history is not one. – hyde Dec 13 '16 at 07:36
  • @hyde MS DOS libraries is mostly just supported by MS DOS compilers. Some ancient Windows compilers did too. Modern Windows compilers won't support it. – Lundin Dec 13 '16 at 07:44
  • @Lundin Ok, dropped from MSVC2015 it seems. However, for example MinGW (just tested with 5.7) seems to support it. I think it does have a niche, which AFAIK is not filled by anything else: Simple toy programs and programming exercises, where you want non-blocking console IO and character graphics withtout hassle. It's even so small, that having learned it can't be considered waste of much time. But for anything else, yeah, don't use it. – hyde Dec 13 '16 at 07:54
  • @hyde Once people pick up bad habits for "learning purposes", you can be 99% sure that they will use the same bad habits later on in production code. – Lundin Dec 13 '16 at 07:57
  • @Lundin I would hope those whose programming knowledge stops at learning to write a Pong-clone using conio.h will stay far away from production code. Sadly, my hope is probably vain. – hyde Dec 13 '16 at 08:05