Forum

> > Off Topic > C - Remove "0" at the end of the sring
Forums overviewOff Topic overviewLog in to reply

English C - Remove "0" at the end of the sring

5 replies
To the start Previous 1 Next To the start

old C - Remove "0" at the end of the sring

GeoB99
Moderator Off Offline

Quote
As in this code below, it prints a timer within a loop which starts the countdown until it reaches 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    /* Variable declaration and initialization */
    int TimeOut;
    TimeOut = 10;
    
    while (TimeOut)
    {
        /* Decrease the countdown */
        --TimeOut;
        
        /* Wait for one second */
        sleep(1);
        
        /* And print the message */
        printf("The countdown in %i\r", TimeOut);

        /* Flush the output buffer (this is needed for \r) */
        fflush(stdout);
        
        if (TimeOut == 0)
        {
            printf("Timeout reached!\n");
            
            /* Kill the loop */
            break;
        }
    }

    return 0;
}
Although there is a problem with the code. When the timeout has been reached (the condition is triggered on
if (TimeOut == 0)
) the zero is also shown at the end of the string even though I did not define that number in the
printf()
. Like this:
Timeout reached! 0


Is there a way to solve this problem? How?
edited 1×, last 26.12.18 01:19:12 pm

old Re: C - Remove "0" at the end of the sring

uaiek
User Off Offline

Quote
Because "Timeout reached!" is not long enough to replace the 0 printed in line 19. If you want to keep the program behavior, then simply add some whitespaces after "reached!" (before \n).

printf("Timeout reached!                       \n");


Here is a shorter version:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <unistd.h>

int main() {
  int timeout = 10;
  while (timeout--) {
    printf("The countdown in %d\r", timeout);
    fflush(stdout);
    sleep(1);
  }
  printf("Timeout reached!           \n");
  return 0;
}

Spoiler >

old Re: C - Remove "0" at the end of the sring

Cebra
User Off Offline

Quote
the zero is from
1
printf("The countdown in %i\r", TimeOut);
in the last iteration,

you don't clear the console, but only overwrite the previous content.

in the last iteration you write:
"The countdown in 0" and then
"Timeout reached!" so that you have to append a few space characters to override the '0'.

the same issue, if you start the coutdown at 11 or higher

edit: @user uaiek: was faster

old Re: C - Remove "0" at the end of the sring

GeoB99
Moderator Off Offline

Quote
@user Cebra / @user uaiek: Filling with some whitespaces at the end of the string solved the problem. Thank you both!

@user Mc Leaf: Using
\r\n
will make the carriage return redundant as it'll do a newline each time the
stdout
stream gets written with output data. I only want the timer string being updated during the cycle of the loop, not the sentence.
To the start Previous 1 Next To the start
Log in to replyOff Topic overviewForums overview