-1

This code is exactly as written in the book I'm using, but the example in the book shows that the third instance of cout in main() should provide the integer -10. Instead it provides 10. Why is my program not providing the actual value given to "glob" in the change_global() function? I'm aware the book is outdated, it is the 4th edition of Beginning C++ through Game Programming.

int glob = 10; // global variable

void access_global();
void hide_global();
void change_global();

int main()
{
    cout << "In main() glob is: " << glob << "\n\n";
    access_global();

    hide_global();
    cout << "In main() glob is: " << glob << "\n\n";

    change_global();
    cout << "In main() glob is: " << glob << "\n\n";
    
    return 0;
}

void access_global()
{
    cout << "In access_global() glob is: " << glob << "\n\n";
}

void hide_global()
{
    int glob = 0;
    cout << "In hide_global() glob is: " << glob << "\n\n";
}

void change_global()
{
    int glob = -10;
    cout << "In change_global() glob is: " << glob << "\n\n";
}
  • 1
    `int glob = -10;` declares and initializes a local variable, distinct from and unrelated to a global variable named `glob` (which I assume exists, though it's not present in the code shown). That global variable remains unchanged. – Igor Tandetnik Jul 19 '20 at 17:52
  • 1
    Helpful reading: https://en.wikipedia.org/wiki/Scope_(computer_science) – user4581301 Jul 19 '20 at 17:53
  • 1
    Sidenote: Learn to program in by books almost always suck, up-to-date or not. Here's [a list of known-good C++ programming books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – user4581301 Jul 19 '20 at 17:56
  • In the beginning I initialize int glob = 10 but it got cut off. The example and the book both claim that when inside of the change_global() func, I have changed the global variable 'glob' and the console displays as such. Maybe this is just a case of the book being terribly wrong. – Kory Stennett Jul 19 '20 at 17:58
  • Your `main()` does not compile because there is no local or global variable `glob`. Also there is output in every function and in `main`. Which output is not as you expect? Are you sure you just copied the code from the book and did not change it? – Werner Henze Jul 19 '20 at 17:59
  • I'm looking at the source code for the book downloaded from here: http://www.delmarlearning.com/companions/content/1305109910/datafiles/index.asp?isbn=1305109910 It is not the same as what you've typed, there is a global variable named `glob` and it is changed by the `change_global` function. Remove the `int` from `int glob = -10;` – Retired Ninja Jul 19 '20 at 18:03
  • I have edited the code to show that I did initialize a global variable `glob`. The code is 100% as the book shows, and the book specifically states I can do this to change `glob` to -10 in main(). – Kory Stennett Jul 19 '20 at 18:03

1 Answers1

0

Posted by Retired Ninja

"I'm looking at the source code for the book downloaded from here: delmarlearning.com/companions/content/1305109910/datafiles/… It is not the same as what you've typed, there is a global variable named glob and it is changed by the change_global function. Remove the int from int glob = -10; – Retired Ninja 38 secs ago"

This solves my issue, thank you all for the help!