functions problem, I dont know how to write the code

hello, this is my first ever comment on the forums.

I am stuck on a piece of coding and not sure how to get around it. I know whats needed (sort of) but not sure how to write it.

So if you could help i would be very thankfull indeed :)

I currently have a tracking program working. However i am now wanting to have a function that will (when a certain button is pressed) minimize the program window and open up another window from a different .cpp file.

i have this written so far but i know it wont work:]

int function(bool start, bool tracking)
{
bool execute;
execute = start==tracking;
return (execute);
}

thats all i have please dont laugh i know its wrong, but i cant get my head around how to do it.

Any help would be great.

thanks

only1skingle
It looks to me like you want to return TRUE if start and tracking are both the same (be it both TRUE or both FALSE). Note that your function claims to return an int, but you actually return a bool.

This code shows the logic of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
bool function(bool start, bool tracking)
{
  bool execute;
  if (start == tracking)
  {
    execute = TRUE;
  }
  else
  {
    execute = FALSE;
  }
  return execute;
}


This code does the same thing, with a bit more elegance.

1
2
3
4
5
6
7
bool function(bool start, bool tracking)
{
  bool returnVal;
  (start == tracking) ?  returnVal = true : returnVal = false ;
  return returnVal;
  
}


Of course, this lends itself to the following evolution:
1
2
3
4
bool function(bool start, bool tracking)
{
 return (start==tracking);     
}


At which point you realise that you don't need a function at all, you can just do what you were first trying to do in your original code, without even needing a separate function. In the code that calls this function, remove calls to the function and just use
execute = (start == tracking);

You were just missing some brackets.

Hope this makes everything nice and clear.

Disclaimer: I very rarely use bool type, and when I do I carry an assumption that the integer zero will be silently cast to false, and any other integer value to true. This assumption may well be in my code above.

Last edited on
ok brill thanks!

one more thnig tho, how would i then write code to have the current program window minimised and have it open up a new window when the returnVal is true?
C++ has no inherent understanding of windows or minimising. For that, you will have to look at the APIs of your chosen operating system; it will be different for each operating system and/or window manager.
right, thanks very much for your help, i appreciate it :)
Topic archived. No new replies allowed.