(Console) I need to output txt, one character at a time to look like somebodies typing.

HI all, I am creating a simulation kind of game that will be programmed for the terminal/console, I am looking for a way to get txt to output as if somebody was typing live, it doesn't have to be perfect as in random timing of output but even a steady flow will do, just to make it a more believable experience :)

It will be for Linux terminal, but even some guidence for windows will do.

I have posted a section of code that would be relevant and to better explain what I want, I want the " hi how are you today " to output as though someone is typing it live.

Can this be done and what would be a good way of doing it ?


 
if(user_input == "hello ") {cout << " hi how are you today" << endl ;} 
Last edited on
If you want to output "hi how are you today" especially when you are typing, you have to use a combination of kbhit() and getche(), which are only available for Windows.

Of course, even if you are able to use these functions, the algorithm will be complex.

Edit :
It is possible for Linux too. You need to copy & paste text from StackOverFlow.
http://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux
Last edited on
@ SakurasouBusters

Thanks for your fast reply, so theres no hope for doing it in Linux then?

It seems a little more complex then I originally thought, I will repost to the advanced forums to see if there is
another way, but thank you very much for the information :)
http://www.cplusplus.com/reference/ostream/ostream/flush/
https://linux.die.net/man/3/usleep


If the idea if for writting character by character, then I think it is horrible.
I don't think you understand what I mean, I don't want to output to a .txt file, its a simualtion, you talk to the bot, the bot talks back, we do not want all of the output txt within the program to magically appear like it does normally, we need the bots output to appear within the console like as if somebody is typing its response before your eyes without you doing anything, the human will then resond normally typing back to it.

Long story short, programmed outputs in response to human input should look like its being typed out!!!!
> It will be for Linux terminal, but even some guidence for windows will do.

We don't need anything more than standard C++ for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <thread>
#include <chrono>

namespace detail_
{
    struct wait_for
    {
        explicit wait_for( unsigned int msecs ) : ms(msecs) {}
        const std::chrono::milliseconds ms ;

        template< typename C, typename T >
        friend std::basic_ostream<C,T>& operator<< ( std::basic_ostream<C,T>& stm, wait_for w )
        { if(stm) std::this_thread::sleep_for(w.ms) ; return stm ; }
    };
}

template< typename C, typename T >
std::basic_ostream<C,T>& operator<< ( std::basic_ostream<C,T>& stm, std::pair< std::string, detail_::wait_for> str_slow )
{
    for( char c : str_slow.first ) stm << str_slow.second << c << std::flush ;
    return stm ;
}

std::pair< std::string, detail_::wait_for> slowly( std::string str, unsigned int ms = 250 ) 
{ return { str, detail_::wait_for(ms) } ; }

int main()
{
    std::cout << slowly( "hello world!\n" ) ; // with default delay of 250 ms between characters
}

http://coliru.stacked-crooked.com/a/102022fc04654fc0
http://rextester.com/TTS2060
> I don't want to output to a .txt file
although the example was using a file, it works the same with the console. They are both output streams, so a lot of functions are shared.
for efficiency, when you send something to `cout' is not immediately written to the screen, but instead send it to a buffer. `std::flush' would say to write the whatever it is in the buffer, not matter how small it is.

> appear within the console like as if somebody is typing its response before your eyes
that's what I think is a horrible idea, ¿how is that a believable experience?
It would be different if you want to wait a time till the response appears, as to simulate the thinking of the other party.
@ne555

Ok thanks for the input, and yes I was considering slowing it down and I might just take that route, I will try out JLborges tip/code above and test both ways :)


(UPDATE) I took the choice of using both, as in, the program will use the realtime like typing at parts along with a slow down of full txt output in response to user input like you said, I think this will make for a more interesting experience :)

Thanks again :)
Last edited on
@ JLBorges, you are a genius, and I sincerely thank you for your help and sharing your code, it works 100% I cannot thank you enough, its programmers like yourself with the skill willing to help others who are new or less experienced in C++ that makes this site all the worth while, thanks again :) :) :)



░░░░░░░░░░░░▄▄░░░░░░░░░
░░░░░░░░░░░█░░█░░░░░░░░
░░░░░░░░░░░█░░█░░░░░░░░
░░░░░░░░░░█░░░█░░░░░░░░
░░░░░░░░░█░░░░█░░░░░░░░
███████▄▄█░░░░░██████▄░░
▓▓▓▓▓▓█░░░░░░░░░░░░░░█░
▓▓▓▓▓▓█░░░░░░░░░░░░░░█░
▓▓▓▓▓▓█░░░░░░░░░░░░░░█░
▓▓▓▓▓▓█░░░░░░░░░░░░░░█░
▓▓▓▓▓▓█░░░░░░░░░░░░░░█░
▓▓▓▓▓▓█████░░░░░░░░░█░░
██████▀░░░░▀▀██████▀░░░░
Last edited on
Topic archived. No new replies allowed.