Global variable???

This is a discussion on Global variable??? within the C Programming forums, part of the General Programming Boards category; Hello all, i'm working on a text based rpg (does'nt everyone) and i need a variable to add to through ...

  1. #1
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    Global variable???

    Hello all, i'm working on a text based rpg (does'nt everyone) and i need a variable to add to through out the function's. Just to show i only come here when i need to i've tryed :
    Code:
    #define B 0
    but when i add to it i get wrong output. My intention is for the user to get more experience as he/she perform's "trick's".
    Any help would be much appreciated.
    Thank's.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    If this is just a simple program, don't you just need to declare your variable outside of your function?

    Like
    Code:
    #include <fstream>
    
    int i_experience=0;
    
    int main (void)
    {
    i_experience=i_experience+25;
    cout << i_experience;
    
    return 0;
    }
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    Confused still...

    Ok now i've tryed :
    Code:
    #include <stdio.h>
    
    #define OLIE 10
    #define FLIP_KICK 50
    #define BIG_JUMP 30
    #define SWITCH 40
    
    int b=0;
    then i add to it:
    Code:
    int curb()
    {
      int z;
      clrscr();
      printf("Were coming up to a waxed curb, what do you want to do, make it fast\n");
      printf("1. do an olie over it.\n");
      printf("2. do a flip kick onto it.\n");
      printf("3. do a big jump over it.\n");
      printf("4. do a switch onto it.\n");
      printf("5. exit\n");
      scanf("%d", &z);
      if ( z == 1 )
      b + 10;
      else if (z == 2)
      b + 50;
      else if (z == 3)
      b + 30;
      else if (z == 4)
      b + 40;
      else
      exit();
      printf("%d", b);
      printf(" point's\n");
      return z; 
    }
    but when i output the value of b it's zero. What's up?
    Last edited by loopy; 05-22-2002 at 12:25 AM.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    If you want to change the variable of b, you need to use the assignment operator =, you can use it like this:

    {pre: b = 0}
    b = b + 10;
    {post: b = 10}

  5. #5
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    Ahhhh...

    Great shiro, i just hope i learned something from this.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >b = b + 10;
    Or the shorthand way like this:
    Code:
    b += 10;
    /* is the same as */
    b = b + 10;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <stdio.h>
    
    #define OLIE 10
    #define FLIP_KICK 50
    #define BIG_JUMP 30
    #define SWITCH 40
    
    int b=0;
    There's no need to assign zero to b. Since it's global that will happen by default.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest
    There's no need to assign zero to b. Since it's global that will happen by default.

    it certainly doesn't hurt anything...it fact i think it's a good idea. it makes the variables initials values more explicit...

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >it certainly doesn't hurt anything
    It doesn't hurt, it doesn't help. Thus it becomes a matter of style, I was merely informing the OP of an alternative.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Don't you think it's good habbit for new coders to get into; to initialise all variables, global or not?

    I understand you're just pointing out that it doesn't have to be done, but for a beginner learning to code, should we be applying an "if it aint broke, don't fix it" rule? Also, I would probably want to know what my global variables are initialised to in a game, since I'd likely have them all in the same place, it would probably aid readability to inialise even the ones I wanted to be zero.

    My logic behind this is the same as my understanding of the logic behind..

    void myfunction (void)
    {
    ....
    }

    as opposed to

    void myfunction()
    {
    ....
    }

    It shows us that the coder explicitly didn't want to pass arguments to that function, as opposed to perhaps having just forgotten.

    There's probably more behind that than I know, and quite likely other reasons / purposes too. I don't claim this is right, just my point of view. I'm interested in other peoples opinions on this, even though it is really a 'style' question.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Don't you think it's good habbit for new coders to get into; to
    > initialise all variables, global or not?

    Yes, always. The only variables that are "safe" to not bother initializing are static variables. Even then, they're initially set to zero, but subsequent uses will contain whatever they held last, so you may need/want to initialize them always before using.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    > Don't you think it's good habbit for new coders to get into; to
    > initialise all variables, global or not?

    In general, explicit initialisations make code more easier to read, especially if defined constants or enum constants are used. Even if automatical initialization is done. Writing readable code is very useful.

  13. #13
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    I understand you're just pointing out that it doesn't have to be done, but for a beginner learning to code, should we be applying an "if it aint broke, don't fix it" rule?
    I posted my code to another board i'm a part of, and one said in so many word's

    . Another, possibly 'cleaner' implementation might be to have the function return the point values themselves, then you could have the function calls increment the total point variables. For instance, you can move the declaration of gl_points into your start() routine, and use:

    gl_points += highpark();

    Then declare a local variable within each subroutine called 'pts' or something similar:

    pts += rail();

    See what I mean? This would be a more 'object oriented' solution, because the function calls are not modifying your global variables. You'd find that if you were to start modifying and expanding this program, that it is often easier if each module is 'self contained' without having to worry about variables that are declared outside itself. I would declare gl_c in each function seperately as well, since it's not being used globally. I know this seems tedious, but you'll find that as the code gets more elaborate, this allows you to keep track of things more easily. It's a good idea to develop good coding habits early on, so you don't become too lazy as things progress.
    So i have two different opinion's, one say's don't modify global variable's to keep thing's 'clean' another say's if it ain't broke. It would be alot easier for me to modify global variable's. What's the genral consensus here.
    Thank's for your time.
    Last edited by loopy; 06-09-2002 at 02:54 PM.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  14. #14
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Never mind, a board search turned up some good result's, anyone interested in the do's and do not's of global variable's look here
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global variable and prototype
    By Lincoln_Poh in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2008, 03:25 AM
  2. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. global variable turns zero
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 04-07-2003, 12:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21