Hey all,

I've elected to write a console hangman game and I've completed it just fine. However, one thing is evading my logic as to why it wouldn't work. I want it to "appear" to LOAD GAME" at the beginning like so: "LOADING GAME....." The total wait time I want it to be is 5 seconds with each 'period' appearing at 1 sec. intervals.

Currently, LOAD GAME appears and immediately goes into the game without any "wait(n)" intervals.

Why won't it work? Here is the relevant parts of my code:

#include <iostream>
#include <string>
#include<conio.h>
#include <ctime>
#include <map>


using namespace std;

void wait (int seconds)
{
clock_t endwait;
endwait = clock() + seconds * CLOCKS_PER_SEC;
while (clock() < endwait) {}
}
int main()
{
    int seconds;
    int countdown = 5;

	cout <<"\n\n\n  Game will begin in 5 seconds" <<endl;
	cout <<"\n\n  Press ENTER to load the game:\n" <<endl;
	getche();

	cout << "\n\t\t\t\tLOADING GAME";
		for (countdown; countdown < 5; countdown++)
		{
			cout <<".";
			wait(1);
		}

Something so simple has me pulling my hair out!


Saw this (below) on a signature that I thought was hilarious:

- ASCII a stupid question, get a stupid ANSI. -

Recommended Answers

All 4 Replies

Currently, LOAD GAME appears and immediately goes into the game without any "wait(n)" intervals.

Why won't it work?

Well, you initialize int countdown = 5 , so that will cause it to bypass the for-loop altogether.

PS. I recently saw;
"When C++ is your hammer, everything starts to look like a thumb."

You set countdown = 5 and when it hits the for loop, the condition is already false (5<5) so it skips the loop completely.

EDIT: Beaten by a hair ^^^^^^^

P.S. Kicking up the warning level on your compiler (so using a -Wall switch with g++ or a /W4 with VC++) would have let you know this

mtirmkar & jonsca,

Thanks for your help! Man, I feel like a real numbnuts initializing int countdown = 5...and on top that, I had no business even declaring int seconds!

Once I deleted the "seconds" and "countdown" dec. and initialization, all went went well and as expected. I now have my totally worthless "LOADING GAME...." effect I was wanting to appear in my console game.


Also jonsca, thanks for letting me know about the -Wall switch. Just started using Code::Blocks last night, after using VS2008, and didn't get a chance to thumb through the options. I REALLY prefer Code::Block over VS2008.

Thanks again all!

You set countdown = 5 and when it hits the for loop, the condition is already false (5<5) so it skips the loop completely.

EDIT: Beaten by a hair ^^^^^^^

P.S. Kicking up the warning level on your compiler (so using a -Wall switch with g++ or a /W4 with VC++) would have let you know this

Just started using Code::Blocks last night, after using VS2008, and didn't get a chance to thumb through the options. I REALLY prefer Code::Block over VS2008.

If you will be using gcc/g++, you can find the on-line documentation -> http://gcc.gnu.org/onlinedocs/

Pick the one that matches your GCC version ( g++.exe -v ).

PS. Don't throw away VS 2008, it may come in handy when seriously debugging something.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.