access data outside a function

Aerion4 (33)
I was wondering what the best way was to access data outside the function it was created in. I have the user input a data in a custom namespace function, and now im trying to access all those data points in a different function. any advice? I can post the code, but it's a little long...
The Mighty Boosh (3)
You might try initializing a global variable before you do "int main()", then, in the other function, set the value you want to have access to elsewhere equal to the global variable.

Be careful though, consider if you need it to be initialized again, besides at the top of the program.
Aerion4 (33)
Tried that global variable thing,

1
2
3
4
5
6

namespace functions
{
	//my .h file
	int collectInput();	
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int functions::collectInput()
{
    // my functions.cpp file
    int a1,b1,c1,a2,b2,c2,x = 0;
    
	cout<<"please enter three numbers seperated by spaces. these will be the A,B,C values\n"
	"for your first quadratic equation."<<endl;
	cin>>a1;
	cin>>b1;
	cin>>c1;
	if(!cin.good())
	{
	cout<<"Bad number, please try again."<<endl;
	exit;
	}
	cout<<"please enter three numbers seperated by spaces. these will be the A,B,C values\n"
	"for your SECOND quadratic equation."<<endl;
	cin>>a2;
	cin>>b2;
	cin>>c2;
	if(!cin.good())
	{
	cout<<"Bad number, please try again"<<endl;
	exit;
	}
	cout<<"enter an 'x' value for both quadratic equations"<<endl;
	cin>>x;
	if(!cin.good())
	{
	cout<<"Bad number, please try again"<<endl;
	exit;
	}

}


all i really want to do for right now, is access a1 b1 c1...etc, outside this function....
Aerion4 (33)
Does anyone else have any insight? I'm just a little lost.
greenleaf800073 (82)
1
2
3
4
5
int functions()
{
      code... place a1, b1 etc. into an array
      return array;
}
Registered users can post here. Sign in or register to post.