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 13:50

Why are you writing bad C in Sepples?

Name: Anonymous 2010-03-13 13:50

Keep repeating on the side of the input resource?

Name: >>2 2010-03-13 19:27

>>2 wasn't a rhetorical question btw.

Name: Anonymous 2010-03-13 20:22

OP here, why isn't the following valid in C++?

bool click(char KEY[], static int n);

I want a variable to behave like a static variable but I don't want the static variable to be shared. In other words, I want each call to my click() function to have its own static variable. Why are static variables shared in the first place?

>>4
That's just an example of my problem. Normally I wouldn't code like that.

Name: Anonymous 2010-03-13 20:25

Then don't code like that and you won't have a problem. If you're writing C++, then write C++ if you're writing C, then take the time to learn C and write it.

Name: sage 2010-03-13 20:36

>>6
Are you asserting that there's a significant difference between C and C++ in the first place to make such a bold statement? As far as I know, C is just C++ without the gimmicks.

On the other hand if that was C# or Java you were talking about I would agree with you. When will C apologist admit that C is inferior to C++ in terms of applicable functionality.

Name: Anonymous 2010-03-13 20:38

>>7
My cURL put sageru in the wrong field. I hate everything.

Name: Anonymous 2010-03-13 21:11

>>7
This looks like a C++ programmer trying to go directly from C++ to C by simply reducing the features used to those available in C, without any refactoring being done. (Alternatively: it is clear that the author does not know C and is not writing C++ code.)

With this in mind, >>6 should make a lot more sense to you now.

Anyway, I made no bold statements. Your Italians must be heavy-set.

Name: >>9 2010-03-13 21:20

>>7
Wow, I just read the second half of your post. Quit being a faggot.

Name: Anonymous 2010-03-13 21:24

It looks like you are trying to create an object.
Do you want help?
 - Learn C++
 - Learn a real programming language
 - Give up computers and become a nun (faggot)
 - Just type the program without help

Name: Anonymous 2010-03-13 21:24

make a static array with an int for each key and increment the corresponding int in the array.

Name: Anonymous 2010-03-13 21:25

>>10
It was not meant to be taken personally or seriously.

Name: Anonymous 2010-03-13 21:28

>>1
Clearly you need to be using the State monad.

Name: Anonymous 2010-03-13 21:30

>>13
It's quite hard to tell with the current infestation.

Name: Anonymous 2010-03-13 21:31

>>7
As far as I know, C is just C++ without the gimmicks.

Please, be trolling.

Name: Anonymous 2010-03-13 21:45

>>12
This seems to be the only solution. Is there a reason why static variables are shared?

Name: Anonymous 2010-03-13 21:57

>>17
?  The what a static variable is.  A variable that is persists through function calls.  It wouldn't make sense to have a static variable for each parameter value.

Name: Anonymous 2010-03-13 22:00

>>18
Why are you helping him?

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.

Name: Anonymous 2010-03-13 22:11

>>18
I want it to be persistent on a local level, not a global level.

Name: Anonymous 2010-03-13 22:26

>>20
a haven for programmers.
What you describe is not a haven.

Name: Anonymous 2010-03-13 22:57

>>22
Hello elitist.

Name: Anonymous 2010-03-13 23:29

>>23
I was only stating a fact. Your attitude is typical of imageboard types.

Name: Anonymous 2010-03-14 1:12

>>24
A classic case of trolls trolling trolls

Name: Anonymous 2010-03-14 1:22

>>21
You realize that what you want is that the static keyword don't work like, you know, a static keyword?

What you want may be:
1. Start programming in C
2. A fork(). A pthread, perhaps.
3. Another static variable inside the function
4. Stick with your b option
5. Do something logical with the array. I mean, why the hell is it an array anyway? If you're not storing anything useful inside it and you want, I don't know, count letters, you could as well do:

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

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

And A and B could be chars or some const variable you could define. Don't know if that is what you were trying to do but your code didn't make much sense as you seemed to pass a char as an argument to a function who expects a char * and make a, at least strange comparison where, if you intended to do as you posted here, I expect for Sussman's sake that you will substitute the 0 for a NULL.
6.Read your SICP

Name: Not >>24 2010-03-14 3:02

>>25
No he's right. Types from the imageboards are scum for the most part.

Name: Anonymous 2010-03-14 3:47

>>20
Simple homework questions should be frowned upon and the user should do them by reading manuals/textbooks or googling. /prog/ should be reserved for more complex questions that can't be trivially answered using a google search. Also, certain boring trolls need to get back to /b/ (HMAmemefan I'm looking at you and the ``faggot'' spoiler one, if they're not one and the same).

Name: Anonymous 2010-03-14 3:59

>>1
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.
Why is it messy? The function needs to store state. Having the caller store it makes the most sense. Compare strtok vs strtok_r.

The only real alternative you have is the Java way where you make an object out of every function that needs to stroke state (e.g. creating a StringTokenizer object just to split a fucking string.)

Your solution is mostly correct. It doesn't make sense for variables in main() to be static though, since that function is only called once. The only other thing you should change is to always use pointers for out parameters instead of references; never take a non-const reference as a function parameter because it's not obvious that the parameter will be modified: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Reference_Arguments#Reference_Arguments

Also, it's nice to see an on-topic thread here for once.

Name: Anonymous 2010-03-14 4:06

The only real alternative you have is the Java way where you make an object out of every function that needs to stroke state (e.g. creating a StringTokenizer object just to split a fucking string.)
Or you could use a closure.

Name: Anonymous 2010-03-14 5:00

>>30
java doesn't have closures

Name: >>30 2010-03-14 5:03


;;; approximate translation of OP's original problem:

(let ((n 0))
  (defun click (key)
    (unless key
      (incf n))
    (when (> n 20)
      (setf n 0))))

(defun main ()
  (cond
    ((click (lookup-key +a+)) ...)
    ((click (lookup-key +b+)) ...)))

;;; a more correct solution with local state

(defun make-click-counter (&optional (times 20))
  (let ((n 0))
    (lambda (key)
      (unless key
        (incf n))
      (when (> n times)
        (setf n 0)))))

(defun main ()
  (let ((click-counter (make-click-counter)))
    (cond
      ((funcall click-counter (lookup-key +a+)) ...)
      ((funcall click-counter (lookup-key +b+)) ...))))
;;; MAKE-CLICK-COUNTER returns a click counter closure
;;; that could could pass around as you'd like. It's a function
;;; with state.

;;; If you intend to use C, you can emulate closures rather simply:
;;; it's just a structure containing a function and an array of arguments
;;; the first argument to the function should be this state array, and the
;;; rest of the arguments are as you wish.
;;; To make it easier on you, you could make a funcall macro which
;;; given a "closure" structure and some arguments expands into
;;; a call to the function in the structure with the array argument
;;; and the rest of the arguments. Instead of an array you may use a
;;; structure, but that will complicate your macro further.
;;; It won't be as pretty as in Lisp, nor as easy to define, but
;;; it should work just fine. It's no harder than defining an object system
;;; in C.

Name: Anonymous 2010-03-14 5:04

>>31
I don't think OP was talking about Java.

Name: Anonymous 2010-03-14 5:12

hax my anus!

Name: Anonymous 2010-03-14 5:13

hax my anus!

Name: Anonymous 2010-03-14 5:13

hax my anus!

Name: Anonymous 2010-03-14 5:13

hax my anus!

Name: Anonymous 2010-03-14 5:13

hax my anus!

Name: Anonymous 2010-03-14 12:02

...use a callback and let the resource handle autorepeating by itself.

Oh, wait, you don't have arbitrary callbacks in SEPPLES

Name: Anonymous 2010-03-14 12:16

>>39
You can use function pointers as callbacks. std::tr1::function allows you to abstract the ugliness of function pointers, but comes with some overhead.

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