Variable Scope/Global Variables

I am trying to calculate the value of e to a given accuracy. I am using a separate file for my function that calculates it. I don't think my function is being used or it is only going through it once.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  #include<iomanip>
#include<iostream>
#include<cmath>
#include<string>

using namespace std;

void find_e();
double tol = 0;
 double e;
 double euler = 2.718281828459045;
 double diff;
 double newe;
string ans = "yes";


int main()
{
	while(ans == "yes")
	{

	
	cout<<"This program will estimate the value of Euler's number to your desired tolerance"<<endl<<endl;
	cout<<"Please input your tolerance: ";
	cin>>tol;
	system("cls");

	if(tol > 0.0)
	{
		
			find_e();

			cout<<"The new value of e is "<<fixed<<setw(12)<<setprecision(10)<<e<<endl;
			cout<<"The actual value of e is "<<fixed<<setw(12)<<setprecision(10)<<euler<<endl;
			cout<<"The difference is "<<fixed<<setw(12)<<setprecision(10)<<diff<<endl;
	}

	else
	{
		cout<<"You can't have a negative tolerance"<<endl<<endl;
	}

	cout<<"Would you like to run this again? yes for yes, any other key for no\n\n";
	cin>>ans;
	}
	system("pause");

return 0;
}



and my function in separate file:

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
35
36
37
38
#include<iomanip>
#include<iostream>
#include<cmath>
#include<string>
using namespace std;


extern double e;
extern double euler;
extern double newe;
extern double diff;
int n = 1;
extern double tol;

void find_e()

{

	do
		{

			e = (n,(1+(1/n)));
		
			diff = abs(newe - e);

			newe = e;
			n*=2;

		

		}
		while(diff>tol);




	return;
}
dont use global variables (.e. move line 14 down to just after line 18).

and certainly don't use extern in this context.

you only use 'euler' in your first file so why even bother referencing it in your 2nd file? and the opposite thing with 'newe' as well. used in the 2nd file but not in the first, so why even reference it in the first file?
Last edited on
I "have" to use global variables in my program for an assignment. I know using all global variables isn't efficient. Also, the only way i know to pass arguments between files is to use extern.
but what i'm trying to say is: you aren't using the same things in 2 files.

remove all the externs from file 2. then make your function take the parameters of interest. also make that function return 'e' so then you can then use that in your main function in file 1.
Last edited on
What do you mean i am not using the same things in 2 files? Am i not calling my function right? Because it seems like that is the problem, or that is is either not computing the value of e or it isn't replacing e = 0 with the new calculated value of e.
I am not supposed to have any arguments that are not global variables nor am i supposed to return a value from the function
Topic archived. No new replies allowed.