-
October 20th, 2014, 11:36 AM
#1
Global variables little help pls
Hi guys I need help with my code this is my homework "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 global variables so you guys can see how it should look
/*#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 with global variables
this is my 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 get a 0 as answer D:
-
October 20th, 2014, 11:41 AM
#2
Re: Global variables little help pls
Just checking: did you teacher stress to you that global variables should be avoided?
Originally Posted by dk_SAM123
I made the program without global variables so you guys can see how it should look
Please post your well formatted code in [code][/code] bbcode tags. Does this program work correctly?
-
October 20th, 2014, 11:52 AM
#3
Re: Global variables little help pls
yes he said we should avoid global variables, but he still wants us to learn how it works
yep the program without global variables works well.
Code:
#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;}
my function.cpp
Code:
#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 my main.cpp
Code:
#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;}
-
October 20th, 2014, 11:56 AM
#4
Re: Global variables little help pls
You did not call the find_e function (which you named f) in the main function.
-
October 20th, 2014, 12:09 PM
#5
Re: Global variables little help pls
so would it look like this?
Code:
#include <iostream>
#include <cmath>
using namespace std;
double n;
double e;
double accuracy;
int main ()
{
void f();
{double euler =2.7182818284590452353602874713527;
e=(1+1/n);
e=pow(e,n);
accuracy=abs(euler-e);
}
cout <<"insert the n value "<<endl;
cin>>n;
cout<<e<<endl;
and function.cpp
Code:
#include <iostream>
extern double n;
extern double e;
extern double accuracy;
void f()
{
double euler =2.7182818284590452353602874713527;
e=(1+1/n);
e=pow(e,n);
accuracy=abs(euler-e);
};
-
October 20th, 2014, 12:14 PM
#6
Re: Global variables little help pls
No, call the function in the main function, not define it in the main function. Furthermore, you need to call it at the right place.
-
October 20th, 2014, 12:19 PM
#7
Re: Global variables little help pls
so would it look like this?
No.
In main() you are using n before you have entered it. You are calling f() to calculate euler and update the global variables yet you are also calculating it in main as well?? All main() needs to do is obtain n, call f() and then display the results.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 20th, 2014, 12:22 PM
#8
Re: Global variables little help pls
yeah i knew that was fishy. How do i call f() inside main from another file though? :/
-
October 20th, 2014, 12:24 PM
#9
Re: Global variables little help pls
Originally Posted by dk_SAM123
How do i call f() inside main from another file though?
Declare it before the main function definition, then call it as per normal, e.g.,
Code:
void f();
int main()
{
f();
}
In a larger program, this declaration of f would go into a header fine.
-
October 20th, 2014, 01:19 PM
#10
Re: Global variables little help pls
Originally Posted by dk_SAM123
yeah i knew that was fishy. How do i call f() inside main from another file though? :/
Have a look at http://www.learncpp.com/cpp-tutorial...ultiple-files/ and the following header files section.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 20th, 2014, 01:33 PM
#11
Re: Global variables little help pls
Your instructions are to call it fine_e. Why are you calling it f?
Your instructions are to calculate e, yet you're initializing a variable with the value you're supposed to calculate. Why?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|