Output Delay and Masking Problems.

I am writing a console based program to show a friend what I can do with C++. My project acts as if the user is speaking to a thinking "computer" and has various moments that pause (using cout.flush(); _sleep();) to mimic the "computer" thinking before it answers. What I would like to do is have each letter of the "computers" response come in a delay, on letter after another, so that it appears as if the "computer" is actually typing it's response. Below is my code thus far. It is unfinished (obviously) because I got the idea for the "typed response" while working. I am also unsure of how to mask the user input for the password (to display an asterisk instead of the inputted letter). I halted progress until I can figure this out...

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <ctime>
#include <string>

using namespace std;

static int waitcounter;
static char startitup;
//static string ;

int main ()
{
	//START-UP
	cout.flush();
	_sleep(7500);
	cout << endl;
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << ". ";
	cout.flush();
	_sleep(1750);
	cout << "Run program 'space2001.exe' (Y/N)?" << endl;
	while (true)
	{
		cout << "C:\\> ";
		cin >> startitup;
		startitup = toupper(startitup);
		
		if (startitup == 'N')
		{
			cout.flush();
	        _sleep(750);
	        cout << ".";
	        cout.flush();
	        _sleep(750);
	        cout << ".";
	        cout.flush();
	        _sleep(750);
	        cout << ".";
			cout << endl;
			cout.flush();
	        _sleep(750);
			cout << "You must play the game. No battles were won without engagement.";
			cout << endl;
			continue;
		}
		if (startitup == 'Y')
		{
			cout.flush();
	        _sleep(750);
	        cout << ".";
	        cout.flush();
	        _sleep(750);
	        cout << ".";
	        cout.flush();
	        _sleep(750);
	        cout << ".";
			cout << endl;
			cout.flush();
	        _sleep(1750);
	        cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
			break;
		}
		if (startitup != 'Y' && startitup != 'N')
		{
			cout.flush();
	        _sleep(750);
	        cout << ".";
	        cout.flush();
	        _sleep(750);
	        cout << ".";
	        cout.flush();
	        _sleep(750);
	        cout << ".";
			cout << endl;
			cout.flush();
	        _sleep(750);
			cout << "Your entry is not valid. Now is not the time to toy around.";
			cout << endl;
			continue;
		}
		}
	cout << "\t";
	cout.flush();
	_sleep(2250);
	cout << "Your";
	cout.flush();
	_sleep(1250);
	cout << " desired";
	cout.flush();
	_sleep(1250);
	cout << " program";
	cout.flush();
	_sleep(1250);
	cout << " is";
	cout.flush();
	_sleep(1250);
	cout << " initializing";
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << ". ";
	cout.flush();
	_sleep(3000);
	cout << "Please";
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(3000);
	cout << " exercise";
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(10000);
	cout.flush();
	_sleep(250);
	cout << " p";
	cout.flush();
	_sleep(250);
	cout << "a";
	cout.flush();
	_sleep(250);
	cout << "t";
	cout.flush();
	_sleep(250);
	cout << "i";
	cout.flush();
	_sleep(250);
	cout << "e";
	cout.flush();
	_sleep(250);
	cout << "n";
	cout.flush();
	_sleep(250);
	cout << "c";
	cout.flush();
	_sleep(250);
	cout << "e";
	cout.flush();
	_sleep(250);
	cout << ".";
	cout.flush();
	_sleep(1000);
	cout << endl << endl;
	int waitcouter = 0;
	while (true)
	{
		waitcounter++;
		if (waitcounter <= 100)
		{
			cout.flush();
			_sleep(250);
			cout << ".";
			continue;
		}
		if (waitcounter >= 100)
		{
			cout << endl;
			break;
		}
		
	}
	
	//USERNAME AND PASSWORD
	cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
	cout.flush();
	_sleep(2000);
	cout << "Hello ";
	cout.flush();
	_sleep(1750);
	cout << "D";
	cout.flush();
	_sleep(750);
	cout << "A";
	cout.flush();
	_sleep(750);
	cout << "V";
	cout.flush();
	_sleep(750);
	cout << "E";
	cout.flush();
	_sleep(750);
	cout << "," << endl << endl;
	cout.flush();
	_sleep(5000);
	cout << "\tI am your 'heuristically programmed algorithmic computer' existing as one of our" << endl;
	cout << "series 9000 units.";
	cout.flush();
	_sleep(1000);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << " The 9000 series is the most reliable computer ever made.";
	cout.flush();
	_sleep(1000);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << " No 9000 computer" << endl;
	cout << "has ever made a mistake or distorted information.";
	cout.flush();
	_sleep(1000);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << ".";
	cout.flush();
	_sleep(750);
	cout << " We are all, by any practical" << endl;
	cout << "definition of the words, full proof and incapable of error." << endl;
	
	
	return EXIT_SUCCESS;
}
Last edited on
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
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <chrono>
#include <thread>

struct with_delay
{
    const std::string text ;
    with_delay( std::string str ) : text(str) {}
    with_delay( const char* cstr ) : text(cstr) {}

    friend std::ostream& operator<< ( std::ostream& stm, const with_delay& delayed )
    {
        static const auto sleep = [] ( unsigned int msecs )
        { std::this_thread::sleep_for( std::chrono::milliseconds(msecs) ) ; };

        for( char c : delayed.text )
        {
            if( std::isspace(c) ) sleep(600) ;
            if( c == '.' ) sleep(400) ;
            stm << c << std::flush ;
            sleep(200) ;
        }
        return stm ;
    }
};

with_delay wait( unsigned int cnt ) { return with_delay( std::string( cnt, '.' ) ) ; }
with_delay operator "" _slowly( const char* literal, std::size_t ) { return literal ; }

int main()
{
    std::cout << "this is the program 'space2001.exe'\n\n"
              << "the program is initialising. " << "please wait "_slowly
              << wait(10) << "\n\n"
              << "I am your 'heuristically programmed algorithmic computer' "
                 "existing as one of our\nseries 9000 units."_slowly << '\n';
}
First off, thank you for taking the time to reply. I still have a problem though. When I try and use your code, I get the following header file c++0x_warning.h . I suppose it's also a good time to mention that I haven't coded in a little over 5 years so, and I should follow my own words here, not having comments in your code is making it a little hard to follow. (EXAMPLE: Should I not use using namespace std; ?). Anyway, here is the contents of the header file I receive.

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
33
34
35
36
37
// Copyright (C) 2007-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file bits/c++0x_warning.h
 *  This is an internal header file, included by other library headers.
 *  Do not attempt to use it directly. @headername{iosfwd}
 */

#ifndef _CXX0X_WARNING_H
#define _CXX0X_WARNING_H 1

#if __cplusplus < 201103L
#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.
#endif

#endif 


A Sidenote... I need to figure out masking as well, if you have any experience.
Another note. If I run my original code as is, I receive no errors. The code compiles and runs perfectly. It's just... well... not what I want.
how to mask the user input for the password (to display an asterisk instead of the inputted letter)


see here for some Windows based ideas: http://www.cplusplus.com/forum/general/3570/
> Should I not use using namespace std; ?

See: http://www.cplusplus.com/forum/general/72248/#msg385442


> This file requires compiler and library support for the ISO C++ 2011 standard.
> This support is currently experimental, and must be
> enabled with the -std=c++11 or -std=gnu++11 compiler options.

Compile with -std=c++11
For example, g++ -std=c++11 -O3 -Wall -Wextra -pedantic-errors
If possible, update the compiler/library to a more current version (GCC 5.3 or above)


With some comments:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <chrono>
#include <thread>

struct with_delay // helper type to print strings with delay between characters
{
    const std::string text ;
    with_delay( std::string str ) : text(str) {}
    with_delay( const char* cstr ) : text(cstr) {}

    // overloaded operator to print the string with delay
    friend std::ostream& operator<< ( std::ostream& stm, const with_delay& delayed )
    {
        // closure (lambda) see: http://www.stroustrup.com/C++11FAQ.html#lambda
        // uses the thread support library: http://en.cppreference.com/w/cpp/thread/sleep_for
        // time duartion in milliseconds: http://en.cppreference.com/w/cpp/chrono/duration
        // type deduction with auto: http://www.stroustrup.com/C++11FAQ.html#auto
        static const auto sleep = [] ( unsigned int msecs )
        { std::this_thread::sleep_for( std::chrono::milliseconds(msecs) ) ; };

        // range based for see: http://www.stroustrup.com/C++11FAQ.html#for
        for( char c : delayed.text ) // for each character in text
        {
            if( std::isspace(c) ) sleep(600) ; // white space: pause for 600 millisecs
            if( c == '.' ) sleep(400) ; // period: pause for 400 millisecs
            stm << c << std::flush ;
            sleep(200) ; // pause for 200 millisecs after each character
        }
        return stm ;
    }
};

// helper function to print cnt number of periods ... with delay
with_delay wait( unsigned int cnt ) { return with_delay( std::string( cnt, '.' ) ) ; }

// user defined literal for convenience. eg. std::cout << "xxy"_slowly ; will print "xyz" with delay between characters
// see: http://www.stroustrup.com/C++11FAQ.html#UD-literals
with_delay operator "" _slowly( const char* literal, std::size_t ) { return literal ; }

int main()
{
    std::cout << "this is the program 'space2001.exe'\n\n"
              << "the program is initialising. " << "please wait "_slowly
              << wait(10) << "\n\n"
              << "I am your 'heuristically programmed algorithmic computer' "
                 "existing as one of our\nseries 9000 units."_slowly << '\n';
}

http://coliru.stacked-crooked.com/a/1e1fa933a324a2ab
Hello @DavidWong

This is a lot less realistic than @JLBorges' code, which has different delays for different types of character. However, the code below is simple (simplistic?) and I've put a few comments in it.

Both threads and timer require C++11. If you are using MinGW's g++ compiler or something similar then you ought to be able to compile with g++ -std=c++11 , but I've never managed to get this to work with threads (and, looking on the internet, I'm not alone). In the end I had to run the compiler that comes with Visual Studio, cl.exe (from the normal Windows command line, not the VS IDE). You can also compile and run the code through cpp-shell from this forum; (gear wheel outside the top right-hand corner of code samples).

If you have some other library that will put in an appropriate delay (and it looks like you have), then just change line 13 and you can do away with threads and chrono.

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
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <thread>             // for this_thread::sleep_for   (needs C++11)
#include <chrono>             // for chrono::milliseconds     (needs C++11)
using namespace std;


//======================================================================


void delay( unsigned int msecs )                                // puts in a delay of msecs milliseconds
{
   this_thread::sleep_for( chrono::milliseconds( msecs ) );
}


//======================================================================


void typedText( string text, unsigned int msecs )               // enters text with a delay of msecs milliseconds between characters
{
   for ( int i = 0; i < text.size(); i++ )      // for each character in text
   {
      delay( msecs );                           // put in a delay
      cout << text[i] << flush;                 // output the character (and flush the buffer)
   }
}


//======================================================================


int main() 
{
  string text;

  text = "Ubuntu Linux\n"     ;   typedText( text, 5   );
  text = "ZX Spectrum\n"      ;   typedText( text, 50  );
  text = "Microsoft Windows\n";   typedText( text, 500 );
  text = "My cat running across the keyboard\n" ;   typedText( text, 1 );
}                                

Last edited on
Topic archived. No new replies allowed.