Thread: Scope Question

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    Scope Question

    Hi,

    I'm having a problem with variable scope. I'm trying to modify a global integer within a function, and it doesn't seem to be working ... I've simplified the code to show what I mean:

    Code:
    static int variable;
    
    void function1(void)
    {
      cout << variable;
    }
    
    void function2(void)
    {
      variable++;
    }
    
    int main(void)
    {
      function2();
      function1();
    
      return(0);
    }
    For some reason the variable isn't getting modified in function 2, because the output in function 1 is the same. Any thoughts? Thanks for the input!!

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    what do u mean its not gettin modified isnt variable start off at zero? it gets incramented to 1.

    i dont see the problem mabye im just not gettin it

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Hi tunerfreak,

    Well, the variable starts out as zero. When I call function 2, it should be incremented to one, but when I call function 1 after function 2, it is still zero, and I'm not sure why.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    it comes out 1 when i run it ????

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Strange. Well here's the real program; it's an OpenGL program:

    Code:
    static int xrot;
    
    void reshape(int w, int h)
    {
      ...
      glRotated(5.0, xrot, yrot, zrot);
      ...
    }
    
    ...
    
    void keyboard(unsigned char key, int x, int y)
    {
      ...
      switch(key)
      {
        case('x'):
        xrot = 1;
        break;
      }
      ...
    }
    For some reason, xrot remains zero, and it's been bugging me for hours!

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    openGL sorry dont know that one

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Ah, it's all good. Thanks though!!

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    switch(key)
    {
    case('x'):
    xrot = 1;
    break;
    Either you declare a local xrot variable so "xrot" there is not the global xrot, 'key' is never 'x', or you reset the value of the xrot variable after the assignment.

    You shouldn't be using a global variable anyway...

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Hi joni,

    Unfortunately, the program requires the global variable. When I replace xrot with 1 in the reshape function, it works fine, so I know the problem is the global xrot not being set to 1 inside the function.

    I've tried placing the rotate function inside the switch statement, and it worked, so I know the key can equal 'x'. There's no other declaration of xrot, nor is there any other assignment of xrot within the program. I'm at a total loss. I'm thinking of just placing all of the commands inside the switch statement just to get it done.

    Thanks for the help joni and tunerfreak, hopefully I'll figure it out!

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    AH! I fixed it with the line:

    Code:
    glutPostRedisplay();
    after my code.

    Thanks everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scope Related question
    By csonx_p in forum C++ Programming
    Replies: 13
    Last Post: 08-24-2008, 11:02 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM