Noob
Noob

Reputation: 121

C++ Flickering When Clearing the Console

I'm trying to make a loading animation for a program I'm making with the C++ console, I have been using system("cls") to clear the console (I know it is bad practice), but the main problem is that the screen flickers when I run the program. My code looks something like this:

Are there a way to fix the flickering? Thanks.

//loop
system("cls");
cout << "Loading..." << num << "%"; 

Upvotes: 0

Views: 728

Answers (1)

Josh
Josh

Reputation: 12722

While you can do as a commenter suggested and use a curses style library, you're probably looking for SetConsoleCursorPosition (2017 docs: https://learn.microsoft.com/en-us/windows/console/setconsolecursorposition)

BOOL WINAPI SetConsoleCursorPosition(
  _In_ HANDLE hConsoleOutput,
  _In_ COORD  dwCursorPosition
);

Parameters

hConsoleOutput [in]

A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see Console Buffer Security and Access Rights.

dwCursorPosition [in]

A COORD structure that specifies the new cursor position, in characters. The coordinates are the column and row of a screen buffer character cell. The coordinates must be within the boundaries of the console screen buffer.

Clear screen/cls is not meant for animations.

Upvotes: 2

Related Questions