Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Array question

Array question

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jw81
    wrote on last edited by
    #1

    Hi I wrote a small program to count how many points are clicked using array. My count array doesnt work even though i wrote the declaration before or in the function itself. For example: int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ } and if((nFlags & MK_LBUTTON) == MK_LBUTTON){ int count; count++; } From what I can see is everytime a left mouse is clicked the array reset itself. The code is inside a OnMouseMove(UINT nFlags CPoint point) class. Thanks

    T H T 3 Replies Last reply
    0
    • J jw81

      Hi I wrote a small program to count how many points are clicked using array. My count array doesnt work even though i wrote the declaration before or in the function itself. For example: int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ } and if((nFlags & MK_LBUTTON) == MK_LBUTTON){ int count; count++; } From what I can see is everytime a left mouse is clicked the array reset itself. The code is inside a OnMouseMove(UINT nFlags CPoint point) class. Thanks

      T Offline
      T Offline
      ThatsAlok
      wrote on last edited by
      #2

      jw81 wrote: int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ actaully problem is of Scope of variable count.it is local variable in function. let see what actually happening in your code-> Every time you click on mouse the count variable created in memory and as function ends, count variable scope end and program release it from memory. now to solve this problem with either declare it in global or make it local to class which handlling you onmousemove function rather declarign it in function.


      "I Think this Will Help" [Vote One Here,.....]

      visit me at http://www.thisisalok.tk
      E J 2 Replies Last reply
      0
      • T ThatsAlok

        jw81 wrote: int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ actaully problem is of Scope of variable count.it is local variable in function. let see what actually happening in your code-> Every time you click on mouse the count variable created in memory and as function ends, count variable scope end and program release it from memory. now to solve this problem with either declare it in global or make it local to class which handlling you onmousemove function rather declarign it in function.


        "I Think this Will Help" [Vote One Here,.....]

        visit me at http://www.thisisalok.tk
        E Offline
        E Offline
        Emilio Garavaglia
        wrote on last edited by
        #3

        ThatsAlok wrote: now to solve this problem with either declare it in global or make it local to class which handlling you onmousemove function rather declarign it in function Or ... let it where it is now, but declare it as static. By the "lifetime" point of view, global or static is mostly the same. Declare it as class member will let every calss instance hace its own. That is: if yor class is used to handle many windows and you want each one to have its own counter, make it a class member. If you want a same single counter to count everything in all instances, declare it as static. In the function if only that function use it; In the class if many function class use it; globally if you have to access it from everywhere. 2 bugs found. > recompile ... 65534 bugs found. :doh:

        T 1 Reply Last reply
        0
        • E Emilio Garavaglia

          ThatsAlok wrote: now to solve this problem with either declare it in global or make it local to class which handlling you onmousemove function rather declarign it in function Or ... let it where it is now, but declare it as static. By the "lifetime" point of view, global or static is mostly the same. Declare it as class member will let every calss instance hace its own. That is: if yor class is used to handle many windows and you want each one to have its own counter, make it a class member. If you want a same single counter to count everything in all instances, declare it as static. In the function if only that function use it; In the class if many function class use it; globally if you have to access it from everywhere. 2 bugs found. > recompile ... 65534 bugs found. :doh:

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          emilio_grv wrote: : if yor class is used to handle many windows and you want each one to have its own counter, make it a class member. i think, it just a beginner problem,so i have given beginner answer. conecpt of static is little complicated for beginner.


          "I Think this Will Help" [Vote One Here,.....]

          visit me at http://www.thisisalok.tk
          1 Reply Last reply
          0
          • J jw81

            Hi I wrote a small program to count how many points are clicked using array. My count array doesnt work even though i wrote the declaration before or in the function itself. For example: int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ } and if((nFlags & MK_LBUTTON) == MK_LBUTTON){ int count; count++; } From what I can see is everytime a left mouse is clicked the array reset itself. The code is inside a OnMouseMove(UINT nFlags CPoint point) class. Thanks

            H Offline
            H Offline
            Hamed Musavi
            wrote on last edited by
            #5

            Hello, I don't understand your problem exactly, but if this declaration is inside a funktion, well you declare a variable and reset it to zero, then ++ it. could you please try using 'static int ... ' instead of your currnt statement? does it solve any problem?

            1 Reply Last reply
            0
            • T ThatsAlok

              jw81 wrote: int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ actaully problem is of Scope of variable count.it is local variable in function. let see what actually happening in your code-> Every time you click on mouse the count variable created in memory and as function ends, count variable scope end and program release it from memory. now to solve this problem with either declare it in global or make it local to class which handlling you onmousemove function rather declarign it in function.


              "I Think this Will Help" [Vote One Here,.....]

              visit me at http://www.thisisalok.tk
              J Offline
              J Offline
              jw81
              wrote on last edited by
              #6

              I guess Alok answered my question, thanks.

              1 Reply Last reply
              0
              • J jw81

                Hi I wrote a small program to count how many points are clicked using array. My count array doesnt work even though i wrote the declaration before or in the function itself. For example: int count=0; if((nFlags & MK_LBUTTON) == MK_LBUTTON){ count++ } and if((nFlags & MK_LBUTTON) == MK_LBUTTON){ int count; count++; } From what I can see is everytime a left mouse is clicked the array reset itself. The code is inside a OnMouseMove(UINT nFlags CPoint point) class. Thanks

                T Offline
                T Offline
                tttyip
                wrote on last edited by
                #7

                I think you need to declare the count variable outside this function, eg. declare it as global or class member variable. If you declare in this function, every time you click the mouse, the counter is reset to zero.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups