CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2014
    Posts
    19

    Question 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:

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Global variables little help pls

    Just checking: did you teacher stress to you that global variables should be avoided?

    Quote 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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Oct 2014
    Posts
    19

    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;}

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Global variables little help pls

    You did not call the find_e function (which you named f) in the main function.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Oct 2014
    Posts
    19

    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);
    	
    };

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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)

  8. #8
    Join Date
    Oct 2014
    Posts
    19

    Re: Global variables little help pls

    yeah i knew that was fishy. How do i call f() inside main from another file though? :/

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Global variables little help pls

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Global variables little help pls

    Quote Originally Posted by dk_SAM123 View Post
    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)

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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
  •  





Click Here to Expand Forum to Full Width

Featured