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

Dani AI

Generated

Quick corrections and a safer approach.

As pointed out, the loop used the misspelled clocks(); the correct function is clock(). was also right to steer toward the C++ headers. Two larger issues are worth highlighting: clock() measures CPU time (not wall-clock time) and spinning in a tight while loop wastes CPU. For a countdown or turn timer, prefer C++11+ timing utilities and avoid busy-waits.

A lightweight, portable pattern is to use std::chrono::steady_clock for wall-clock intervals and std::this_thread::sleep_for to avoid burning CPU. For console input with a timeout, std::async + future::wait_for is a simple cross-platform option (with caveats below).

// wait without busy-waiting
#include <chrono>
#include <thread>

void wait_seconds(int sec)
{
    using namespace std::chrono;
    auto end = steady_clock::now() + seconds(sec);
    while (steady_clock::now() < end)
        std::this_thread::sleep_for(milliseconds(50));
}
// simple timed read (returns empty string on timeout)
#include <future>
#include <string>
#include <chrono>
#include <iostream>

std::string read_line_timeout(unsigned timeout_sec)
{
    auto fut = std::async(std::launch::async, [](){
        std::string s;
        std::getline(std::cin, s);
        return s;
    });

    if (fut.wait_for(std::chrono::seconds(timeout_sec)) == std::future_status::ready)
        return fut.get();

    return std::string(); // timeout
}

Notes and design tips: make move() return a status (e.g., bool or enum) so the caller can skip the turn on timeout; avoid new for simple ints — use stack variables; do not rely on cancelling a blocked std::getline thread (some implementations leave that thread blocked), so for a robust game loop prefer non-blocking OS-specific console I/O or an event-driven GUI where timers are natural.

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 () 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.