How would I output text one letter at a time like it’s typing without using Sleep() for every character?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sleeping is the best way to do what you’re describing, as the alternative, busy waiting, is just going to waste CPU cycles. From the comments, it sounds like you’ve been trying to manually hard-code every single character you want printed with a sleep call, instead of using loops…
Since there’s been no indication that this is homework after ~20 minutes, I thought I’d post this code. It uses
usleepfrom<unistd.h>, which sleeps for X amount of microseconds, if you’re using Windows trySleep().Since
stdoutis buffered, you’re going to have to either flush it after printing each character (fflush(stdout)), or set it to not buffer the output at all by runningsetbuf(stdout, NULL)once.The above code will print
"hello world\n"with a delay of 100ms between each character; extremely basic.