How to skip a block of commands when the user interrupts?

I am working on a program that does something like this,

Void main()
{
cout<<"H";timedelay(1);
cout<<"E";timedelay(1);
cout<<"L";timedelay(1);
cout<<"L";timedelay(1);
cout<<"O";timedelay(1); //timedelay(int a) is a function which gives a delay
// of 'a' seconds.
{
....
}
}


This code is just for fancy and I would like to squish in some statements which would give the user an option to skip it (by entering any keyboard key),and resume with the rest of the program.
Please suggest from your knowledge and experience,on techniques to get this done!
Last edited on
1. No portable way
2. int main, please.
Why int main()?
What is wrong with void main()?
I don't want it to return anything.
Why int main()?


Because that's what the C++ language requires.

What is wrong with void main()?


It's nonstandard because it disobeys the laws of the language.

I don't want it to return anything.


main() is special. You don't have to explicitly return anything from it. If you omit the return statement, it will assume main returns 0.
Thank you very much!
Well this exposes the limits of my own C++ knowledge, but isn't there a standard way to check if there's any keystrokes in the keyboard buffer? If so, you could check within your timedelay() function and if a keystroke is found, return a value which main() could use to skip the remaining output.

perhaps using something like http://www.cplusplus.com/reference/istream/istream/peek/
Last edited on
but isn't there a standard way to check if there's any keystrokes in the keyboard buffer?
No.
You should use system-dependend ways to do that.
in a serious program I might agree. But for exercise purposes, what wrong with something like:
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
using namespace std;
using namespace std::chrono;

...

bool timedelay( double sec )
{
	auto start_time = steady_clock::now();
	auto stop_time = steady_clock::now();

	duration<double> time_span = duration_cast<duration<double>>( stop_time - start_time );

	char key = ' ';

	while( time_span.count() < sec )
	{
		stop_time = steady_clock::now();
		time_span = duration_cast<duration<double>>( stop_time - start_time );

		// check for any keyboard input
		key = cin.peek();
		if( key == EOF )
			return false;
	}
	return true;
}


note: I haven't tested the above yet. So if it plain just doesn't work I'll understand.
Last edited on
ok, nevermind. cin.peek() seems to want to stop and wait for input. seems silly to me.
You are mixin up keyboard buffer and input buffer which are different things. In consoles input is generally (a) blocking and (b) buffered.
I know that the keyboard buffer isn't the input buffer, and it just occurred to me that my head is once more impacted in my anus for thinking that the input buffer would magically get filled from the keyboard buffer behind the scenes :) Thanks for the lube.
Topic archived. No new replies allowed.