Solved

Global variables

Posted on 2011-03-21
4
349 Views
Last Modified: 2012-05-11
Hi guys: Can any one please tell me how to i define the variable number as a global variable ? Also how to pass the number variable value to the getDoubleNumber function ?

Thanks.

#include <iostream>
using namespace test;

int getDoubleNumber();

int main()
{
      int number       = 0;
      int doubleNumber = 0;
      cout << "Enter a number: ";
      cin >> number;

      doubleNumber = getDoubleNumber();
      cout << "Answer: " << doubleNumber << endl;

      system("pause");
      return 0;
}
int getDoubleNumber()
{
      return number * 2;
}

0
Comment
Question by:mustish1
  • 2
4 Comments
 
LVL 2

Assisted Solution

by:niralbhavsar
niralbhavsar earned 150 total points
ID: 35186052
This is what you requested:
#include <iostream>
using namespace test;

int getDoubleNumber();
int number       = 0;

int main()
{

      int doubleNumber = 0;
      cout << "Enter a number: ";
      cin >> number;

      doubleNumber = getDoubleNumber(number);
      cout << "Answer: " << doubleNumber << endl;

      system("pause");
      return 0;
}
int getDoubleNumber(number)
{
      return number * 2;
}

Open in new window

Its preferred if you don't use a global variables for elegant programming. You don't need a global variable if your code looks like this:
#include <iostream>
using namespace test;

int getDoubleNumber();


int main()
{
	  int number       = 0;
      int doubleNumber = 0;
      cout << "Enter a number: ";
      cin >> number;

      doubleNumber = getDoubleNumber(number);
      cout << "Answer: " << doubleNumber << endl;

      system("pause");
      return 0;
}
int getDoubleNumber(number)
{
      return number * 2;
}

Open in new window


0
 

Author Comment

by:mustish1
ID: 35186093
still get a syntax error on line
int getDoubleNumber(number)
-------------------------------------------
#include <iostream>
using namespace test;

int getDoubleNumber();
int number       = 0;

int main()
{

      int doubleNumber = 0;
      cout << "Enter a number: ";
      cin >> number;

      doubleNumber = getDoubleNumber(number);
      cout << "Answer: " << doubleNumber << endl;

      system("pause");
      return 0;
}
int getDoubleNumber(number)
{
      return number * 2;
}
0
 
LVL 8

Accepted Solution

by:
crysallus earned 350 total points
ID: 35186225
That code from niralbhavsar is a little incorrect. You need to include the data type of the argument in the function declaration and definition:

#include <iostream>
using namespace test;

int getDoubleNumber(int);

int main()
{
          int number       = 0;
      int doubleNumber = 0;
      cout << "Enter a number: ";
      cin >> number;

      doubleNumber = getDoubleNumber(number);
      cout << "Answer: " << doubleNumber << endl;

      system("pause");
      return 0;
}
int getDoubleNumber(int number)
{
      return number * 2;
}

Open in new window

0
 

Author Closing Comment

by:mustish1
ID: 35188705
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now