I have selected to do a hangman console game in my C++ course (9th week) in which I have a question about wait().

I want to make the game "appear" to be loading just to give it aesthetics but I'm not sure how to do it the way I want to do it. Initially, I had it display, "LOADING GAME....", then my next line was "wait (5)" to make it appear it was loading for 5 seconds.

Now, what I want to do is have the periods after "LOADING GAME" to delay 1 second before consecutively appearing. Here is what I want to do but obviously, the syntax is incorrect but it's the concept I want to convey.

The code is incomplete as I am compiling and running incrementally to ensure each coded parts run w/o error.

The particular part, in question, is within the single IF statement:

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

using namespace std;

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

int main()
{

	int seconds;       //
	char guess;        //user input
	const char BLANKS = "-";   //used as a placeholder for letters not yet guessed
	string
	char answer[7];    //holds the answer
	char hidden[7];    //holds dashes ("-") for hidden letters
	int right = 0;     //0 = wrong guess & 1 = right guess
	int count = 0;
	int countdown = 5;
	string MSG_YES = "Good Job! You got it!.";
	string MSG_HUNG = "Sorry, but you were hung.";
	string ANSWER = "kitchen";

	cout <<"\t**************************" <<endl;
	cout << " " <<endl;
    cout <<"\t\tHANGMAN!" <<endl;
	cout << " " <<endl;
	cout <<"\t\t\t**************************" <<endl;

	cout <<"\n\nDIRECTIONS:" <<endl;
	cout <<"\n\n1. Try to guess the computer's mystery word. " <<endl;;
	cout <<"\n2. Guess your letter one at a time." <<endl;
	
	cout <<"\n\n\n  Game will begin in 5 seconds" <<endl;
	cout <<"\n\n  Press ENTER to begin the countdown:\n" <<endl;
	getche();

		for (countdown; countdown > 0; --countdown) 
		{		
			cout << countdown << " seconds \n";      //countdown decrement to be displayed on console.
			wait (1);                               //1 second between decrements
		}
   	
		if (countdown == 0)                        //determines when to display LOADING GAME...
		{
			cout << "\n\t\t\t\tLOADING GAME."wait(1)"."wait(1)"."wait(1)"."wait(1)"\n\n";	
			//wait(5);    //waiting 5 seconds to give "appearance" of game loading 
		}

	cin.get();
	return 0;
}

Recommended Answers

All 7 Replies

the mechanism of calling the wait () function is faulty.

cout<<"Loading \n" ;
for(int i=0;i<4;i++)
{
cout<<"." ;
wait(1);
}

would do what you are expected of.

You seemed to have understood the concept of function calling /basic syntax wrongly.

Example to get you started:

#include <ctime>
#include <iomanip>
#include <iostream>

using namespace std;

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

int main()
{
    const int dots = 3;
    const int n = 5;

    cout<<"LOADING";

    for (int i = 0; i < n; i++)
    {
        cout.put('.');
        wait(1);

        if ((i + 1) % dots == 0)
        {
            // Reset the dots
            cout<< setw(dots) << setfill('\b') <<""
                << setw(dots) << setfill(' ') <<""
                << setw(dots) << setfill('\b') <<"";
        }
    }

    cout<<"\nDONE!\n";
}

Sky Diploma,

I knew the syntax for the wait() function was wrong in the IF statement and I noted that above. I wrote it that way just to convey the concept of what I wanted to do. I was just unsure how to do it to get my desired result.

Is that what you meant by my misunderstanding?

the mechanism of calling the wait () function is faulty.

cout<<"Loading \n" ;
for(int i=0;i<4;i++)
{
cout<<"." ;
wait(1);
}

would do what you are expected of.

You seemed to have understood the concept of function calling /basic syntax wrongly.

I meant that

cout << "\n\t\t\t\tLOADING GAME."wait(1)"."wait(1)"."wait(1)"."wait(1)"\n\n";

Would cause errors during compilation.

Instead the for loop would do a better job :)

Sky,

I forgot to thank you helping me! It accomplished what I wanted it to. However, because I don't know, why is the wait () function faulty? Is there an alternative?

the mechanism of calling the wait () function is faulty.

cout<<"Loading \n" ;
for(int i=0;i<4;i++)
{
cout<<"." ;
wait(1);
}

The wait function isn't faulty at all.

The way it was being called in the above mentioned line was wrong/faulty.

That's what i meant to say :)

Ah, thanks for clarifying it to me. I was misinterpreting your meaning.

The wait function isn't faulty at all.

The way it was being called in the above mentioned line was wrong/faulty.

That's what i meant to say :)

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.