【问题标题】:Printing a string letter by letter and with delay [closed]Printing a string letter by letter and with delay [closed]
【发布时间】:2022-12-01 19:42:32
【问题描述】:

I'm new to programming and I'm learning two languages simultaneously (Python and C++).

In the Python version of the program I was making, I wrote a function that given a string, and a speed, prints out a string letter by letter, and with a given speed:

def timedprint(txt, speed):
    for c in txt:
        print(c,end='')
        time.sleep(speed)

Then, for example, I could do this:

timedprint('::::::::::Fahrenheit to Celsius / Celsius to Fahrenheit converter::::::::::\n', 0.025)

This will print that string, letter by letter, with a separation of 0.025s each letter.

Now I'm trying to write the same code but using C++ but I cannot find a solution. Is it possible to do the same exact thing I'm doing in Python, but in C++? In a discord server, someone gave me this:

Iterating over characters of a string is easy, then you just need to sleep. See:

https://en.cppreference.com/w/cpp/thread/sleep_for

std::string str = "hello";
for (char c : str) {
std::cout << c;
}

But I don't understand, and it is not what I asked for.

Also found this in this forum:

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

int main() {
  using namespace std::chrono_literals;

  std::cout << "H" << std::flush;
  std::this_thread::sleep_for(500ms);
  std::cout << "e" << std::flush;
  std::this_thread::sleep_for(500ms);
  std::cout << "l" << std::flush;
  std::this_thread::sleep_for(500ms);
  std::cout << "l" << std::flush;
  std::this_thread::sleep_for(500ms);
  std::cout << "o" << std::flush;
  std::this_thread::sleep_for(500ms);
}

This does the job, but imagine if I want to print a long text; it will take many lines of code and I'm sure there must be an easy way, just like in Python.

【问题讨论】:

  • C++ has loops just as python does.
  • Please invest in some good C++ books.
  • "But I don't understand, and it is not what I asked for." yes thisiswhat you asked for. Why do you not understand it?
  • did you try to use the loop you were suggested to use? did you try to add a sleep inside the loop? Whats wrong about it? If you do not understand it you need to tell us what you do not understand, because an answer here cannot tell much more than what you were already told

标签: c++ loops for-loop printing


【解决方案1】:

The modern approach in C++ is to use std::string and loop over the elements with a for-loop:

void slow_print(std::string str) {
    using namespace std::chrono_literals;

    for (const auto& i : str) {
        std::cout << i << std::flush;
        std::this_thread::sleep_for(500ms);
    }
}

Example usage:

int main() {
    slow_print("Hello");
}

【讨论】:

  • note that this is already in the quesiton, but OP says that it isnt what they are asking for (i suppose the real issue is something not mentioned here, because this is what they ask for ;)
猜你喜欢
  • 2022-12-16
  • 2013-01-29
  • 2022-12-16
  • 2013-03-04
  • 2016-03-16
  • 1970-01-01
  • 2022-12-02
  • 2016-06-16
  • 2019-10-16
相关资源
最近更新 更多