C - Remove "0" at the end of the sring
5 replies



26.12.18 11:32:25 am
As in this code below, it prints a timer within a loop which starts the countdown until it reaches 0.
Although there is a problem with the code. When the timeout has been reached (the condition is triggered on
Is there a way to solve this problem? How?
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
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;
}
#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
Already tried with escape sequenze '\r' instead of '\n'?
Also you could try '\r\n' instead.
Also you could try '\r\n' instead.

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.
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).
Here is a shorter version:
Code:
printf("Timeout reached! \n");
Here is a shorter version:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}
#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;
}
It seems to be good.
the zero is from
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: @
uaiek: was faster
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: @

loading...
@
panteon00 / @
uaiek: Filling with some whitespaces at the end of the string solved the problem. Thank you both!
@
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. Hey,
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.

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:
Outlast 2 Modification (27)





