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

5 replies
Goto Page
To the start Previous 1 Next To the start
Up
Fraizeraust
Moderator
Offline Off
As in this code below, it prints a timer within a loop which starts the countdown until it reaches 0.
Code:
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:
Code:
Timeout reached! 0


Is there a way to solve this problem? How?
edited 1×, last 26.12.18 01:19:12 pm
26.12.18 12:01:05 pm
Up
Mc Leaf
Super User
Offline Off
Already tried with escape sequenze '\r' instead of '\n'?

Also you could try '\r\n' instead.
user DC has written:
I'm actually working on Stranded III now with highest priority even though I said that I planned to release the next CC update first.
26.12.18 12:31:10 pm
Up
uaiek
User
Offline Off
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).

Code:
printf("Timeout reached! \n");


Here is a shorter version:
Code:
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 >
It seems to be good.
26.12.18 12:35:46 pm
Up
panteon00
User
Online On
the zero is from
Code:
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
loading...
26.12.18 01:31:54 pm
Up
Fraizeraust
Moderator
Offline Off
@user panteon00 / @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.
26.12.18 05:29:32 pm
Up
Masea
Super User
Offline Off
Hey, user Fraizeraust! You can always go to Stack Overflow and ask your questions about coding there. It'd be quicker to get the better answers you're looking for, I believe.

I'm not being a jerk who goes a bit off-topic, it is just an idea of mine.
Create your UI easy and fast: UI Framework | Go deeper into the darkness and test your bravery: file cs2d Outlast 2 Modification (27)
To the start Previous 1 Next To the start