0

I'm trying to simulate a loading screen that goes from 0% to 100%.

I'm using Console.Write instead of WriteLine but this only prints the value alongside the rest instead of replacing the last print.

for (int k = 0; k <=100; k++){
                    Console.Write($"[LOADING: {k}% ]");
                    Thread.Sleep(25);
                }

This results in:

[LOADING 1%] [LOADING 2%] [LOADING 3%] [LOADING 4%] [LOADING 5%]

I expect a single string that replaces itself with the next printed value

2 Answers2

2

Just call Console.Clear() in each iteration of the for loop to write over any text that was previously displayed in the console.

for (int k = 0; k <=100; k++){
                Console.Clear()
                Console.Write($"[LOADING: {k}% ]");
                Thread.Sleep(25);
            }
Luke
  • 838
  • 7
  • 17
0

Try this

for (int k = 0; k <=100; k++){
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.Write($"[LOADING: {k}% ]");
                Thread.Sleep(25);
            }