Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ [Help Appreciated]

Name: Anonymous 2010-03-13 13:43


bool click(char KEY[])
{
    static int n;
    if(KEY!=0)n++;
    if(n>20){n=0; return true;}
    else return false;
}

int main()
{
    if(click(key[A]))//Do something
    else if(click(key[B]))//Do something
    return 0;
}


How do I make it so that the value of "static int n" is only incremented by one instance of the click() function? I used printf to watch the value of "int n" and it was being shared by both instances of the click() function.

One solution I found that worked was by passing a separate variable to each function:


bool click(char KEY[], int &n)
{
    if(KEY!=0)n++;
    if(n>20){n=0; return true;}
    else return false;
}

int main()
{
    static int a=0, b=0;
    if(click(key[A], a));//Do something
    else if(click(key[B], b));//Do something
    return 0;
}

Again, this works but it's kind of messy. Surely there must be a better solution to this problem. As you can guess, I'm new to this.

Name: Anonymous 2010-03-13 22:09

>>19
It's my dream to turn /prog/ into a haven for programmers.  Where university students can come for solutions to their homework, where newbies can learn about things such as static variables and abstract classes, and where redditers can come to share interesting tid-bits such as why multiple inheritance is considered harmful. This is my dream.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List