I have some question about printing on C. Here is my code.
while(1)
{
if(Button == 32)
{
break;
}
else
{
if(Button == 1)
count += 1;
if(btn_value == 2)
count -= 1;
if(btn_value == 4)
count = 0;
printf("%d\r", count);
fflush(stdout);
}
}
for (int i=count;i>=0 ; i--)
{
printf ("YOUR TIME : %02d\r", i);
fflush(stdout);
sleep(1);
}
This is part of my main code. What I designed is to receive button input and change the value of the count correspondingly. What I want to do is to display the value of count
in one line that continuously reflects change of count
.
I mean, if current situation is
Result window
2
and if I give Button = 1, I want result to be
Result window
3
not
Result window
2
3
So I found some code to do this but when count exceeds 10 and return back to one digit(0~9), the result shows
10 to 90, not 09. Also not 08 but 80. How can I handle this?
You can overwrite leftovers by spaces, like this:
printf("YOUR TIME : %d \r", i);
Do you see the space character between %d
and \r
?
Some notes:
%02d
, but this does not match your description of the wrong output.\r
at the beginning of the format string, as it seems more logical to me.\r
where it is now, and printf("FINISHED \n");
or puts("FINISHED ");
with enough spaces.