1

I want to make a quizgame with a countdown. The problem is; when I use system cls all the prints are going. I tried using printf("/ b"). In that case, I can't get data from the user because the system is in the loop.

Can I keep the question output and count down and get input from the user?

Here this is my countdown code:

int v=30;
while(v!=0) {
    printf("\n\t%d",v);
    v--;
    sleep(1);
    system("cls");
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    Would it be an option to store all the output you want to keep across the `cls` and just output it again afterwards? – Yunnosch Dec 23 '20 at 22:45
  • Or avoid `cls` altogether, it is just outputting a terminals worth of newlines to scroll existing text up -- quite unnecessary. You can use ANSI escapes. Both windows and Linux terminals support them (but understand, that's about as far a compatibility can be assured). The actual requirement is a terminal that supports VT emulation. – David C. Rankin Dec 23 '20 at 23:00
  • "count down and get input from the user" --> not with standard C and its library. Code needs to use other functions. – chux - Reinstate Monica Dec 23 '20 at 23:05
  • suggest using [alarm()](https://stackoverflow.com/questions/1784136/simple-signals-c-programming-and-alarm-function) for countdown. [ncurses tutorial](https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/) for cursor positioning and text output – user3629249 Dec 25 '20 at 15:14

2 Answers2

0

If I understand your question, and you just want to display a countdown in the same location on the screen, then for terminals that support VT100 emulation (and some earlier VTXX versions), you can just use ANSI escapes to control the cursor visibility and a carriage-return ('\r') to return cursor position to the original starting point. If you use the field-width modifier for your integer output, you don't even need the ANSI escape to clear to end-of-line. If you have variable number of characters that are part of your countdown, you can use the ANSI escape to clear to end-of-line to ensure all text is erased each iteration.

For example you could do:

#include <stdio.h>
#include <unistd.h>

int main (void) {
    
    int v = 30;
    
    printf ("\033[?25l");                           /* hide cursor */
    while (v--) {
        printf ("  countdown: %2d\r", v);           /* print, CR */
        fflush (stdout);                            /* flush stdout */
        sleep (1);
    }
    printf ("\033[?25h\n");                         /* restore cursor, \n */
}

If you did have additional text after the countdown number that varied in length with each iteration, you could use:

        printf ("  countdown: %2d\033[0k\r", v);    /* print, clear to end, CR */

which includes the clear to end-of-line escape \033[0k.

The two additional ANSI escapes used above are \033[?25l (hide cursor) and \033[?25h (restore cursor).

The fflush(stdout); is necessary because output in C is line-buffered by default. Without fflush(stdout);, all output would be buffered until a '\n' was encountered -- making all text appear at once.

Give it a try. If you have a VT compatible terminal, it will work fine. But note, the reason ANSI escapes are discouraged is they are not portable. Not all terminals support VT emulation (but a lot do...) See ANSI Escape sequences - VT100 / VT52 for additional escape sequences.

If you are developing a full fledged terminal app with numerous inputs and outputs formatted on the screen, you are better served using a library that provides that capability, such as ncursees etc..

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
-1

The possible soulution: I won't put the sleep. I would ask the user inside the loop and after every answer i would v-- .

#include <stdio.h>

int main (void) {
    int v=10;
    char *name;
while(v!=0){
    printf("Whats your name? ");
    scanf("%s", &name);
    printf("\nyour name is %s", &name);
 printf("\n");
printf("\n\t%d",v);
 printf("\n");
v--;

}
    return 0;
}
ElsaKombat
  • 19
  • 2
  • 1
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Dec 23 '20 at 22:55
  • 1
    `char *name; ... scanf("%s", &name);` is certainly a problem. Mis-match type, no space for the _string_ that was read. – chux - Reinstate Monica Dec 23 '20 at 23:00
  • 1
    Code in an answer should at least be properly formatted. – Jabberwocky Dec 23 '20 at 23:22