Happy Birthday Code Problems

I am writing a program for my friend on his birthday, and in the beginning it looks like Glados (play portal-its a robot) overrides my code. Is there a way to put a wait function or something like that that will make it look like I am conversing with Glados. For instance, I say something, it waits five seconds, then Glados' words appear on the screen. Also, is there a way to make it so the users don't have to press enter after entering input?
Here's a pretty basic 5 second count example, though you'll need to adapt it to your own needs (I'm in the middle of some messy OpenGL stuff right now, so I knocked this up kinda quick).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
	bool running = true;
	time_t start, update;
	start = time(NULL);

	while(running)
	{
		update = time(NULL);

		if ((update-start) > 5)
		{
			running = false;
		}
	}
	cout << "5 seconds have passed. The cake is a lie." << endl;
	cin.ignore();
	return 0;
}


EDIT: Here's the same thing in a function. You can just pass in the number of seconds you want to wait for:
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
#include <iostream>
#include <time.h>

using namespace std;

void wait(unsigned int seconds)
{
	bool stall = true;
	time_t start, update;
	start = time(NULL);

	while(stall)
	{
		update = time(NULL);
		if((update-start > seconds))
			stall = false;
	}
}

int main()
{
	wait(5);
	cout << "5 seconds have passed. The cake is a lie." << endl;
	cin.ignore();
	return 0;
}


Might want to add some validation to make sure you don't pass negative numbers into the function (you'll have like a 4 billion second wait otherwise!). That said, it seems to be for a basic, one-off thing so I wouldn't worry too much.
Last edited on
k thx
Topic archived. No new replies allowed.