### Global Variables Problem/Question

Basically, I am having some trouble with my Global Variables, I have them declared within multinumbers () and they will display the correct values accordingly. But when I call multinumbers () inside int main () they reset back to 0. Since they were delcared inside multinumbers () should calling them in main work or am I missing something? I apolgize for the obnoxious couts it's late ha.

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include using namespace std; int first = 0; int second = 0; int final = 0; void multinumbers () { cout << "FIRST NUMBER BRO:" ; int first = 0; cin >> first; cout << "SECOND NOW LOL:" ; int second = 0; cin >> second; int final = first * second; cout << "display from multinumbers(): "; cout << first << "x" << second; cout << "=" << final << endl; } int main () { cout << "This program will help yo ass with numbas" << endl; multinumbers (); cout << "display from main ():"; cout << first << " x " << second; cout << " = " << final << endl; return 0; }``````
Last edited on
NO,, a global variable , is declared only ONCE,, outside main,, why did you declare it again inside multinumbers() ?

 ``1234567891011121314151617181920212223`` ``````#include using namespace std; int first = 0; int second = 0; int final = 0; void multinumbers () { cout << "FIRST NUMBER BRO:" ; int first = 0; // This should be deleted, this is what causes your problem cin >> first; cout << "SECOND NOW LOL:" ; int second = 0; // This should be deleted too cin >> second; int final = first * second; cout << "display from multinumbers(): "; cout << first << "x" << second; cout << "=" << final << endl; }``````

and using global variables when there are other way should be avoided
Last edited on
Hi James180,

First, please always use code tags - they make it easier to read and have line numbers which we can refer to. So Edit your last post, select all the code, then press the <> button on the right under the format menu. If you do it right it should look like the code in my link below. Some other advice just so you know, is to post any compiler messages in full, they show line numbers & tell us exactly what is going on.

 http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/

Your problem is a good example of why NOT to use global variables. If you re-declare a variable in a function (including main() ) then that variable is local to that function - it is not the same variable as the global one.

So move your variable to main(), send variables to which ever function needs them. Either return a value from a function & assign it to a variable in main, or use references.

It might be that the need only be local to a function, that is, not needed elsewhere. In that case local variables are fine, just return the answer.

 http://www.cplusplus.com/doc/tutorial/

Also the is untold info in the reference section & lots of articles to read.
Thanks to both of you for the solution and extra material I am only about 3 days into C++, so thanks for dealing with the silly mistakes!
Topic archived. No new replies allowed.