Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to create a command-line progress bar in C/C++

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The task is to write a C/C++ program to draw a command-line progress bar.

Approach: To create a progress bar the idea is to use system() function which will give colored output. Below is the illustration of how to use system() function.

The system function accepts the following parameters for colouring the output screen:

  1. keyword: color
  2. Background color
  3. Foreground color

Color Codes: 

ColorColor Code
BLACK                   0
BLUE1
GREEN2
CYAN3
RED4
MAGENTA5
BROWN6
LIGHT GRAY7
DARK GRAY8
LIGHT BLUE9
LIGHT GREEN10
LIGHT CYAN11
LIGHT RED12
LIGHT MAGENTA13
YELLOW14
WHITE15
BRIGHT GREENA
BRIGHT CYANB
BRIGHT REDC
BRIGHT MAGENTAD
BRIGHT YELLOWE
WHITEF

Syntax:         

system(“color 9F”); 
The above code will give White Color output with Bright Blue color on Background. 

Below is the program to draw the progress bar in the command line in C/C++: 

C




// C program to create loading bar
#include <stdio.h>
#include <windows.h>
  
// Function to creating loading bar
void loadingBar()
{
    // 0 - black background,
    // A - Green Foreground
    system("color 0A");
  
    // Initialize char for printing
    // loading bar
    char a = 177, b = 219;
  
    printf("\n\n\n\n");
    printf("\n\n\n\n\t\t\t\t\t"
           + "Loading...\n\n");
    printf("\t\t\t\t\t");
  
    // Print initial loading bar
    for (int i = 0; i < 26; i++)
        printf("%c", a);
  
    // Set the cursor again starting
    // point of loading bar
    printf("\r");
    printf("\t\t\t\t\t");
  
    // Print loading bar progress
    for (int i = 0; i < 26; i++) {
        printf("%c", b);
  
        // Sleep for 1 second
        Sleep(1000);
    }
}
  
// Driver Code
int main()
{
    // Function Call
    loadingBar();
    return 0;
}

C++




// C++ program to create loading bar
#include <iostream>
#include <windows.h>
using namespace std;
  
// Function to creating loading bar
void loadingBar()
{
    // 0 - black background,
    // A - Green Foreground
    system("color 0A");
  
    // Initialize char for printing
    // loading bar
    char a = 177, b = 219;
  
    printf("\n\n\n\n");
    printf("\n\n\n\n\t\t\t\t\t"
           + "Loading...\n\n");
    printf("\t\t\t\t\t");
  
    // Print initial loading bar
    for (int i = 0; i < 26; i++)
        printf("%c", a);
  
    // Set the cursor again starting
    // point of loading bar
    printf("\r");
    printf("\t\t\t\t\t");
  
    // Print loading bar progress
    for (int i = 0; i < 26; i++) {
        printf("%c", b);
  
        // Sleep for 1 second
        Sleep(1000);
    }
}
  
// Driver Code
int main()
{
    // Function Call
    loadingBar();
    return 0;
}

Output:
Below is the output of the above program:


My Personal Notes arrow_drop_up
Last Updated : 19 Jul, 2020
Like Article
Save Article
Similar Reads