samedi 26 juin 2021

How to slowdown an output character by character? (Mac OS)

My goal is to output a string of characters one by one. But when I run it, it staggers and ends up just outputting the entire string with no delay in-between characters. I am currently running this code on a Mac operating system.

#include <iostream>
#include <thread>
#include <chrono>

/**
    Asks user to input name.
    @param usrName Displays the the prompt asking for their name.
*/
void displaySequence(std::string usrName);

int main() {
    std::string prompt = "Name: ";
    std::string clientName;

    displaySequence(prompt);
    std::cin >> clientName;
    

    return 0;
}

void displaySequence(std::string usrName) {
    for (int i = 0; i < (int) usrName.length(); i++) {
        std::cout << usrName.at(i) << " ";
        std::this_thread::sleep_for(std::chrono::milliseconds(20));
    }
}

Aucun commentaire:

Enregistrer un commentaire