Hi, I am new to the forum. I am in the learning process and I will stick around until I learn everything from you.

I try to create count down starting at 15 seconds. But when I compile, "clock identifier not found." I am !ondering the same thing, too. Perhaps can you explain how it works and give me correct it for me? I learn it faster by that way. Thanks!

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

void move()
{
	int countDown;
	for (countDown = 15 ; countDown > 0; countDown--)
	{
		counter(1);
	}
}
Ancient Dragon commented: Great Going! Used code tags correctly on very first post :) :) +36

Recommended Answers

All 6 Replies

You didn't post the entire program, so I assume you failed to include the header file that declared clock() function.

You didn't post the entire program, so I assume you failed to include the header file that declared clock() function.

Here is everything. I try to rewrite Gunbound (gunbound.com) along as I learn.

#include "stdafx.h"
#include <iostream>
#include "time.h"

using namespace std;

void counter(int seconds)
{
	clock_t endTurn;
	endTurn = clock() + seconds * CLOCKS_PER_SEC;
	while (clocks() < endTurn){}
}

class Mobile
{
	public:
		Mobile(int startHealth, int startDamage, int moveDistance, int turnDelay):
		  Health(startHealth),
		  Damage(startDamage),
		  Distance(moveDistance),
		  Delay(turnDelay)
		  {};

		void move()
		{
		int countDown;
		for (countDown = 15 ; countDown > 0; countDown--)
		{
			counter(1);
		}


			int * uInput = new int;
						
			while (uInput - Distance < 0)
			{
				cout << "" << endl;
				cin >> *uInput;
			}
			delete uInput;
			uInput = 0;
		}

		void attack()
		{
			int uAttack = 0;
		}

		void defense()
		{
			int uDefense = 0;
		}
	private:
		int Health;
		int Damage;
		int Distance;
		int Delay;
};

class Weapons
{
public:
	void SingleShot()
	{
	}
	void DoubleShot()
	{
	}

private:
	int WeaponDelay;
};


int main()
{
	Mobile KnakMachine(1000, 250, 100, 600);
	KnakMachine.move();


	char f;
	cin >> f;
	return 0;
}

>>while (clocks() < endTurn){}

misspelled. clock() instead of clocks()

also in c++ its ctime and not time.h, for the header.

also in c++ its ctime and not time.h, for the header.

Thanks!

Also, I've been trying to research how to hop out of a function when a condition is met. Looking at the code above, if the player hasn't finished his moved in 15 seconds he would lose his turn. Then he should be kicked off move() function. Any ideas?

"Also, I've been trying to research how to hop out of a function when a
condition is met"

if(condition is met) return false; //assuming function is bool returned typed

int main()
 {

while(Alive)
   {
     if( checkConditionIsMet() == false) return out of this function.
   //some stuff
   }

}
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.