Home c++ Serial output to the C++ console

Serial output to the C++ console

Author

Date

Category

I am writing a program in C++.
It is required that the line is displayed with pauses, i.e. letter, delay, letter, delay, etc.
Tell me how to write this, please.


Answer 1, authority 100%

For example, like this:

void outDelay (const std :: string & amp; s, int ms)
{
  for (auto c: s)
  {
    std :: cout & lt; & lt; c;
    std :: this_thread :: sleep_for (std :: chrono :: milliseconds (ms));
  }
}
int main (int argc, const char * argv [])
{
  outDelay ("Hello", 500);
}

Answer 2

there are many ways, you can delay the flow, but you can not, for example:

# include & lt; iostream & gt;
#include & lt; string & gt;
#include & lt; chrono & gt;
using namespace std :: chrono;
void timedCout (const std :: string & amp; str, double ms)
{
  high_resolution_clock :: time_point t1 = high_resolution_clock :: now ();
  int i = 0;
  std :: cout & lt; & lt; str [i ++];
  while (i & lt; str.size ())
  {
    high_resolution_clock :: time_point now = high_resolution_clock :: now ();
    duration & lt; double & gt; time_span = duration_cast & lt; duration & lt; double & gt; & gt; (now - t1);
    if (time_span.count () & gt; = ms)
    {
      t1 = now;
      std :: cout & lt; & lt; str [i ++];
    }
  }
}
int main (int argc, char * argv [])
{
  std :: string str = "timedcout";
  timedCout (str, 0.6f);
  return 0;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions