Ask coding questions

← Back to all posts
How to add color and slowprint in C++
CODER34352 (7)

I have a question.

How do you add color to a C++ text and make it go in Slow print?

Answered by Coder100 (16911) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (16911)
#include <unistd.h>
#include <iostream>

void sp(std::string str, int time) {
  for (char c : str) {
    std::cout << c << std::flush;
    usleep(time * 1000);
  }
}

Time is in milliseconds, like most things.

CODER34352 (7)

@Coder100
Thank you,
I found all the code in your other comment about slowprint[C++]
#include <iostream>
#include <string>
#include <thread>
#include <chrono>

using namespace std;

void slow_print(const string& message, unsigned int millis_per_char=10) {
for (const char c : message) {
// flush is used to make sure the console is empty
cout << c << flush;

// Ask the thread to sleep for at least n millis.
this_thread::sleep_for(chrono::milliseconds(millis_per_char));

}
}

int main() {
cout << "\033[0;34m" << endl;
slow_print("Welcome To the Karate Dojo", 50);
}
I found the other code about color in a website after years of looking.

Coder100 (16911)

Lol

Mark as correct answer? @CODER34352