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.
#include <iostream>
#include <cmath>
usingnamespace 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_
//main.cpp
double e;
int main (void) { /* something */ }
And then use the extern keywork in your other file
1 2 3 4
//function.cpp
externdouble 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.