0

i have this code and the user need to write a number in one second the problem is i dont know how to stop the scanf after one second,

for example : the user press enter to start a timer will start in the same time i want to break the while after one second the problem is scanf doesnt stop after 1 second

ps :it works when the user doesnt enter anything.


  int i = 0;
  int max = 0;
  int num;
  int counter = 0;

  while (i != 10) {

    char ch;
    printf("Hit enter when ready\n");
    fgetc(stdin);

    scanf("%c", &ch);

    if (ch == 0x0A || ch != 0x0A) {
      num = -1;
      DWORD start_time = GetTickCount();
      DWORD check_time = start_time + 1000;
      printf("ENTER KEY is pressed.\n");
      while ((check_time > GetTickCount())) {
        if (kbhit()) {
                //fflush(stdin);
          scanf("%d", &num);

          break;
        }
      }

      if (num == -1) {
        printf(" :(\n");
      } else {
        printf("%d\n", num);
        counter++;
        if (num > max)
          max = num;
      }

    }

    i++;
  }
  printf("you entred %d numbers\n", counter);
  printf("the biggest number is %d\n", max);
}```
Chihab
  • 33
  • 8
  • Please add some punctuation to you problem description and reword it. It is not clear. – Eugene Sh. Dec 07 '22 at 16:29
  • This is not possible in plain ISO C. However, most platforms provide the functionality that you require as platform-specific extensions. Therefore, if you want your question to be answerable, you will have to specify to which platform (i.e. operating system and compiler) your question applies. – Andreas Wenzel Dec 07 '22 at 16:32
  • the platform is windows with the compiler GNU GCC – Chihab Dec 07 '22 at 16:37
  • @Chihab: This is important information that should be added to the question itself. Please [edit] your question to add it. It should not be necessary to read the comments section to get all the required information to answer your question. – Andreas Wenzel Dec 07 '22 at 16:38
  • 1
    Does this answer your question? [How can I prevent scanf() to wait forever for an input character?](https://stackoverflow.com/questions/21197977/how-can-i-prevent-scanf-to-wait-forever-for-an-input-character) – Andreas Wenzel Dec 07 '22 at 16:43
  • [My answer](https://stackoverflow.com/a/72394274/12149471) to the duplicate question linked above gives you the answer for the Microsoft Windows platform. Most of the other answers apply to Linux/POSIX. – Andreas Wenzel Dec 07 '22 at 16:44
  • Unfortunately, my solution linked above will require you to handle the individual keypresses yourself. This means that all the basic input handling must be handled by you. For example, if the user presses the backspace key to delete the previous character, then you will have to program it yourself, if you want the previous character to be deleted. So what you are asking for is possible, but may require additional programming work. – Andreas Wenzel Dec 07 '22 at 16:54
  • yeah i dont think that it is the solution that i want – Chihab Dec 07 '22 at 16:56
  • @Chihab: I'm afraid that it is the only solution in your case, unless there is a third party library that offers the functionality that you require. (I don't know of any, but they probably exist.) – Andreas Wenzel Dec 07 '22 at 17:01
  • @Chihab: If you use my solution, you will need to build the input string yourself, using the input characters reported by `ReadConsoleInput`. You can then use the function [`strtol`](https://en.cppreference.com/w/c/string/byte/strtol) to convert the input string to a number. – Andreas Wenzel Dec 07 '22 at 17:09
  • You are only giving the user a single second to enter a number? That seems like a very short time period. Do you maybe mean a single "digit" instead of a "number"? In that case, the solution is simpler, as you will not have to build a string or use `strtol`. – Andreas Wenzel Dec 07 '22 at 17:12
  • yes just a signle second not a single degit – Chihab Dec 07 '22 at 20:50

0 Answers0