473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to create a global variable?

I am trying to create a global variable in C++. I am fairly new to C++ and keep getting an error message my getDoubleNumbr function does not recognize the number variable. I know that the error is because the number vaiable is local to the main function. To fix this error i am trying to create a global variable.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //declare global variable
  5. int numbeer = 0;
  6.  
  7. //function prototype
  8. int getDoubleNumber();
  9.  
  10. int main()
  11. {
  12.     //declare variables
  13.     int number       = 0;
  14.     int doubleNumber = 0;
  15.  
  16.     //get input item
  17.     cout << "Enter a number: ";
  18.     cin >> number;
  19.  
  20.     //double the number
  21.     doubleNumber = getDoubleNumber();
  22.  
  23.     //display result
  24.     cout << "Answer: " << doubleNumber << endl;
  25.  
  26.     system("pause");
  27.     return 0;
  28. }    //end of main function
  29.  
  30. //*****function definitions*****
  31. int getDoubleNumber()
  32. {
  33.     return number * 2;
  34. }    //end of getDoubleNumber functi
on
Feb 25 '11 #1
4 3405
whodgson
542 512MB
Your global variable is 'numbeer'.
Your local to main() variable is 'number'.
Feb 25 '11 #2
so how do i make the fix so that number is the global variable and not the local variable? i want the program to run without giving me the error message.
Feb 25 '11 #3
whodgson
542 512MB
change the word number to numbeer in lines 18 and 32
Feb 25 '11 #4
horace1
1,510 Expert 1GB
in C++ you can have a global variable and a local variable using the same identifier and use the scope resolution operator :: to access the global variable, e.g.
Expand|Select|Wrap|Line Numbers
  1. //declare global variable
  2. int number = 10;
  3.  
  4. int main()
  5. {
  6.     //declare variables
  7.     int number       = 20;
  8.     cout << "local number " << number << " global number " << ::number << endl;
  9.  }
  10.  
when run gives
Expand|Select|Wrap|Line Numbers
  1. local number 20 global number 10
no recommended as it can lead to confusing and difficult to debug code, i.e. you forget the ::
Feb 25 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
7
by: Carlos | last post by:
Ok, I am a VB developer but need to do a C# program, my question is how do I decalre a global variable in c#, in VB.net I juest create a procedure an declare my Variable as Public myVar and I can...
9
by: ruca | last post by:
How can I declare a global variable in my .js file, that I can preserve her value each time I need to call any function of .JS file in my ASP.NET application? Example: var aux=null; function...
3
by: Ralph | last post by:
Hi Is this possible? One thing I can do is create the global array and add the new item to it from inside the function but that's not what I'm looking for. Thank you Ralph
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
14
by: Fred Block | last post by:
Hi All, I'm an experienced VB6 developer and now starting (newbee) with VB 2008 and I'm very excited. Here's an issue I'm experiencing right off the starting line and cannot make sense of it:...
2
by: ncsthbell | last post by:
At a form level I believe I can create a variable as" Public v_name as string and this variable would be available to all events on the form. (I think!) I would like to create a 'global'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.