Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>#include &lt;Conio.h&gt; #include &lt;mutex&gt; // C++11, if not C++11 find your own mutex std::vector&lt;char&gt; inputBuffer; std::mutex inputGuard; struct Locker { std::mutex* m; Locker(std::mutex&amp; m_):m(&amp;m_) { m-&gt;lock(); } void release() { if (m) m-&gt;unlock(); m = 0; } ~Locker() { release(); } } void AddToInputBuffer( char c ) { Locker lock(inputGuard); inputBuffer.push_back(c); printf("%c",c); } std::string FinishInputBuffer() { Locker lock(inputGuard); std::string retval( inputBuffer.begin(), inputBuffer.end() ); inputBuffer.clear(); printf("\n"); return retval; } void OverlappedPrintln( std::string s ) { Locker lock(inputGuard); for (int i = 0; i &lt; inputBuffer.size(); ++i) { printf("%c", 8 /*backspace*/); } printf("%s\n", s.c_str()); for (int i = 0; i &lt; inputBuffer.size(); ++i) { printf("%c", inputBuffer[i]); } } std::string readCharactersFromUser() { // TODO: Handle exiting while( int input = getch() ) { if (input == '\n') { return FinishInputBuffer(); AddToInputBuffer(input); } } </code></pre> <p>Now, replace <code>cin.getline(message, 256);</code> with <code>std::string message = readCharactersFromuser()</code>, then check the length of the <code>string</code> before doing <code>(message[0] == 's' &amp;&amp; message[1] == 'a' &amp;&amp; message[2] == 'y' &amp;&amp; message[3] == ' '</code> that.</p> <p>Elsewhere in the program, where you do <code>printf</code>, instead build a <code>std::string</code> and call <code>OverlappedPrintln</code> with it.</p> <p>The effect of this is that when you want to print something out, you lock a mutex, backspace over the user input, print out the message, do a newline, then print out the text you backspaced over, and release your mutex. The mutex isn't needed in 99% of situations, especially with modern computer speed, but it is good practice.</p> <p>The above code was not tested or compiled. Nor is it of the highest quality.</p> <p>The choice of non-echoing get character was because I cannot read from user input in a lock, and asynchronously echoing to the output might cause garbled text. So I read without echo, then echo within the mutex, such that <code>inputBuffer</code> always contains the characters that the user has both typed and I've echoed to the screen.</p> <p>This is an implementation of @NikBougalis 's answer.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload