GLOBAL VARIABLES HELP PLS

Hi guys I need help with my code I have to write this program "Write a C++ program that will calculate the value of “e” (Euler’s number, e = 2.71828…) using a function you create named “find_e”. It shall have no arguments and return no values using the return statement. All transfer of information to and from the function must be in the form of global variables. The function “find_e” must be contained in a file separate file the file containing the “main” function . All output must be from the main program"

I made the program without using using global variables across multiple files to show you guys how it should look like.
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
#include <iostream>
#include <cmath>


using namespace std;

int main ()

{
	double e;
	double n;
	double accuracy;
	int euler = 2.7182818284590452353602874713527;

	cout <<"insert the n value "<<endl;
		cin>>n;

		e=(1+1/n);
		e=pow(e,n);


		cout<<e<<endl;

		accuracy=abs(euler-e);

		cout<<"the accuracy is "<<accuracy<<endl;
		system ("pause");

return 0;}





and my code with global variables is this

this is my global.h
[/code]
#ifndef _GLOBAL_H_
#define _GLOBAL_H_

extern double n;
extern double e;
extern double accuracy;
extern double euler = 2.7182818284590452353602874713527;

#endif

[/code]

this is my func.cpp
[/code]

#include "global.h"
#include <iostream>


double n;
double e;
double accuracy;
double euler;


void f()
{



e=(1+1/n);
e=pow(e,n);

accuracy=abs(euler-e);

}

[/code]

and this is my main.cpp
[/code]
#include <iostream>
#include <cmath>
#include "global.h"

using namespace std;

int main ()

{


cout <<"insert the n value "<<endl;
cin>>n;


cout<<::e<<endl;



cout<<"the accuracy is "<<::accuracy<<endl;
system ("pause");

return 0;}


[/code]

Please guys help me the only example in the book is not helpful at all ,and the only thing professor did was read the example on the book ;/
You have to declare the variable in main.
1
2
3
4
//main.cpp
double e;

int main (void) { /* something */ }


And then use the extern keywork in your other file
1
2
3
4
//function.cpp
extern double e;

/* find_e function */


Your problem is that (1) you didn't declare the variable in main. (2) You used the extern keyword in globals.h (which is correct), but you also redeclared it in functions.cpp.
so this is what I have now. I removed the header file.

/*function.cpp

#include <iostream>


extern double n;
extern double e;
extern double accuracy;
double euler =2.7182818284590452353602874713527;


void f()
{

e=(1+1/n);
e=pow(e,n);

accuracy=abs(euler-e);

};

*/

and main.cpp

/*
#include <iostream>
#include <cmath>


using namespace std;

double n;
double e;
double accuracy;


int main ()

{


cout <<"insert the n value "<<endl;
cin>>n;


cout<<::e<<endl;



cout<<"the accuracy is "<<::accuracy<<endl;
system ("pause");

return 0;}

*/

I just a 0 as answer D:
Topic archived. No new replies allowed.