Functions in "if" statements?

Hi,
I just wrote the classic tictactoe game, everything works fine, and now i want to use timers that can be toogled on/off before the game.
1
2
3
void stimer() //start/set the timer
{now = clock();
deadline = now + 5*CLOCKS_PER_SEC;}

1
2
3
4
bool ftimer()//finish the timer
{now = clock();
if (deadline>now){return TRUE;}//if player was in time its true, else false
else {return FALSE;}}

1
2
3
4
bool iftimer()
{char ans;ans=getch();//this would be the toogle funct
if (ans=='y'||ans=='Y'){return TRUE;}
else return (FALSE);}

1
2
void clastmove()//if timer is on it cancels the last move if it was out of time
{if(iftimer()==TRUE){table[x-1][y-1]=' ';}}

1
2
void ctimer() //and finally the check if player is out of time, so it deletes the mark.
{if (ftimer()==FALSE){cout << "Time out! Next player.\n";clastmove();}}


Well, maybe there are much better ideas for this timer problem, but hell ya.
I place the functions like this:
1
2
3
4
stimer(); //set the timer 
cin >> x_coord; cin >> y_coord; ftimer(); //after user input check if user stepped in time or not
*draws the new 'X' or 'O' in table*
ctimer();//if user was not in time it cancels his move. 


So, when the time is out, clastmove() is executed, it checks if the timer is on, if on it cancels the last move. But when clastmove() is executed - if(iftimer()==TRUE) - the iftimer is also, and it asks again if i want to turn on the timer.

How can i do that an "if(***)" only reads the return value of the function, nad not executes it again, and i only have to toogle it on at the start of program, and then the other functions only read the value "true" if i answered 'y', or "false" if i said 'n'.

I'm only learning and practicing c++, so my programs are not well organized, but they do what they have to.
ps:sorry for my english.

EDIT1:if the pc is unbelievably slow that it can only happen in nightmares, so the call of ctimer also calls the ftimer(i think) in the if statement, and it replaces the value of the ftimer called instantly after user input or not?

EDIT2:i know the solution of placing ans=getch() in the main and the iftimer() only reads it, so if there arent any other solutions let me know and i close this stupid topic:P
Last edited on
My suggestion honestly is have confidence in yourself and use an API, the main three are SFML, SDL, and Allegro.

They are all great libraries, don't concern yourself over which is the best.

Only things to keep in mind:
SFML- doesn't work with C, but it's very neat looking when reading it
SDL - Is C based and has lower level details than Allegro and SFML, eventually you'll have to learn OpenGL to accompany it.
Allegro - Haven't used it, all I know is it is also C based, and seems to work on a lot platforms. I have heard pretty good things about it.

The problem with using standard C++ for this is your programs will inherently be very linear. It would be a lot easier if you could have your game constantly check things but it's just going to sit there at the input unless you can do some multi-threading, which is new and doesn't have much support.

I used to always tell myself that I wouldn't try learning an API for games or anything else for that matter, until I was half-way capable with standard C++. However, I personally believe using standard C++ for games and hoping to learn something will only slow you down. Learn the basic concepts and get working with an API, you aren't hurting yourself doing so. If you really are sure you aren't ready for an API, I.e you don't know about classes and structs, and haven't static_cast before, then you'll have to hold off on some ideas. Try to keep writing programs that can function in a linear style until you've got most of the basics down pat to the point you can sit down and code for a while without constantly referencing.
SFML actually has bindings for several other languages, including C.
http://www.sfml-dev.org/download/bindings.php#csfml
Huh, I knew it had some bindings for Java, .NET and some others, didn't know about CSMFL though. Thanks for pointing that out, I'll make sure to remember that in the future.
I heard about APIs, but i dont know what are they. I can use struct and classes well, and a little casting.
So, if i dont missunderstand it, api is like programming in a visual way, i dont have to program eg. the window and buttons myself, they are already programmed and i can just use them? Its like a new header which makes it easier to use a language? Correct me:D
Also im planning to be a programmer, maybe game programmer, and i heard games are programmed in c++, now im 17 so i have much time to learn, should i continue c++ and learn multi-threading or chose an API?
Thanks for the answers!
and one more: what is better for 3D rendering, in speed, platform supports, vga support ...ect..:direct x or open gl, i heard very different opinions.
Also what is easier to start with?
Topic archived. No new replies allowed.