1

I'm trying to create simple program that requests a user to input a number but in the upper section I display a clock that updates every second.

Here's what I know

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

int a;

int main(void) {
    int a =1;
    while(a)
    {
    system("cls");
    time_t rawtime;
    struct tm*  time_;

    time(&rawtime);
    time_ = localtime(&rawtime);

    printf("%i:%i:%i %i %i %i\n", time_->tm_hour, time_->tm_min, 
            time_->tm_sec, time_->tm_mday, time_->tm_mon+1,
            time_->tm_year+1900);
    printf("Give the input :");
    scanf("%d",&a);
            }
    return 0;
}

I took the printing time code from Program a simple clock in C

What my code does is print the time and then it waits for the input, but it doesn't update the clock until I give the input.

Is there any possible way to do what I want or what keyword do I needed to search the solution? I'm sorry if my English broken, but if what I say isn't clear enough just run the code :).

Community
  • 1
  • 1
Albert H M
  • 257
  • 2
  • 15
  • 6
    isn't scanf blocking? You seem to be in need of threading. – Sourav Ghosh May 02 '17 at 08:45
  • 1
    What system are you working on? On Unix-like systems, take a look at the `ncurses` library for full-screen text-mode applications. – ilkkachu May 02 '17 at 08:46
  • 1
    You could use `select` in a loop to check if any input was entered or not and proceed accordingly. Or just use `pthread`s as suggested above. – Spikatrix May 02 '17 at 08:52
  • @SouravGhosh, yes the scanf blocking, because of that i dont know how to do it... so how can i do that? i dont know what should i search in google or any reference book – Albert H M May 02 '17 at 08:59
  • @ilkkachu well thanks for the info, i working in ubuntu... what i ask is simplified version of what i really face, because i need to make the user can input some variable while there are background system working counting the time and when the time is over it will give an output...(sorry if i confusing you) – Albert H M May 02 '17 at 08:59
  • @CoolGuy okay, i will looking for it... – Albert H M May 02 '17 at 09:01

3 Answers3

1

There are only two ways to display something while waiting for input:

  • use non blocking IO and poll for user input while constantly updating the displayed time. That will be easy with a GUI library or through non portable system calls - sadly non longer standard portable C
  • use 2 threads, one for updating the display, the other for user input. It is almost portable since C11, except that the support for C threads is optional. And it will lead to a much more complex program

TL/DR: Even if it looks simple (and was indeed possible with basic language in the 80' on any personnal computer), non blocking terminal IO is far from simple in C language because of the assumption that the terminal is just a special case of IO.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • well, thanks for the suggestion, do you have any recommendation where could i learn C into that level(threading, nonblocking etc.) (link or book)? i have learn all C basic i know(until struct) but i dont know where to learn C more advance.. – Albert H M May 02 '17 at 09:31
0

Your problem is simple: you can't wait for an user input and do something else meanwhile, unless you use threads. May be what you could do is to wait for an input for a certain amount of time, print time and loop.
Otherwise, just know that using thread is not really complicated, but it will increase significantly the complexity of your program which purpose seemed to remain simple.

Badda
  • 1,329
  • 2
  • 15
  • 40
0

What you want is "non-blocking I/O".

How do you do non-blocking console I/O on Linux in C?

There is an answer in the above linked question that has a code snippet. The accepted answer also states that:

you pretty much don't do non-blocking I/O

and if you have to, you will

simplify this another way, by putting the console I/O into a thread or lightweight process.

The code snipped is hideously complicated and in my experience not guaranteed to work.

Community
  • 1
  • 1
Martin B.
  • 1,567
  • 14
  • 26
  • i use system("cls") because i dont know how to refresh the display... if there any way to do what i desire without using system("cls") is okay.. i will implant this code in linux in the real problem.. // okay i will looking for it, thanks – Albert H M May 02 '17 at 09:24