[LC++]declaring and using manipulating global variables

hursh hursh at infonet.isl.net
Fri May 16 10:53:02 UTC 2003


Is this what you're after?

#include <iostream>

int i = 0;

void inc(){ i++; }

int main(){
         int x = 5;
         do{
                 inc();
                 std::cout << i << std::endl;
         }while(x--);
         return 0;
}

Dan

Julien Patrick Claassen wrote:
> Hi!
>   My unhappiness. I want to use a global variable for counting up an
> array-index. But now all functions that deal with my array have to get it as
> a parameter. That makes me unhappy, because most of them, don't even do
> something with it directly (they may just pass it along for another
> "subfunction" they call. Besides, I thought there was a way to declare a
> global variable, that is known everywhere and can be changed everwhere. Being
> changed meaning, changing it globally. I simply tried it with something like:
> 
> #include <something>
> int glob_index;
> (functions here using glob_index);
> int main()
> {
>   glob_index = 6;
>   change_glob_index(); // being a function that manipulates glob_index
>   return 0;
> }
> 
>   I used some simple output to see the value of glob_index and saw, that it is
> only changed locally.
>   Is there a - let's say - more elegant way of doing this? Or is my way the
> usual?
>   Kindest regards
>         Julien




More information about the tuxCPProgramming mailing list