Reading\Writing a function from a file

This is a discussion on Reading\Writing a function from a file within the C++ Programming forums, part of the General Programming Boards category; Basically, what i am trying to do, is write a program that will integrate a function using the rectangle method. ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    24

    Reading\Writing a function from a file

    Basically, what i am trying to do, is write a program that will integrate a function using the rectangle method.

    I'm fine with the maths part.

    And i can declare a function within the actual file. What i would love to do, in the end is to be able to run the program, be able to input any function (of x - say x^2, or x^3 - 3x for example), and then integrate this by the chosen method.

    At the minute, i can only change the function within the cpp pile, and not within the program.

    Any suggestions would be great. I am a fairly newcomer to C++ so sorry if i don't grasp what is needed!!

    I thought maybe i could get the program to first ask u to input the function, and save that in a seperate file, and then later on it can call on that saved file and read from it the function thats been inputted.

    Anyway, here is what i have:

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <math.h>
    #include <fstream.h>
    
    float funcj(float);
    
    float main()
    
    {
    
            int n, i;
            float a, b, h, x, s, integral;
    
                                
            i = 0;
            
                    cout << "Please input a, the lower bound:  ";
                    cin >> a;
                    cout << "\nPlease input b, the upper bound:  ";
                    cin >> b;
    
                            if (a >= b)
    
                            {
    
                            cout << "\nYou have inputted the data incorrectly. Please try again.\n";
    
                            }
    
                                    else {
    
                                            integral = 0;
    
                                            cout << "\nPlease input the number of strips required:  ";
                                            cin >> n;
    
                                                           {
                                                    
                                                            h = fabs(a - b) / n;
            
                                                            while ( i < n)
                                                            {
                                                            
                                                            x = a + i * h;
            
                                                            s = h * funcj(x);
                                                            
                                                            integral = integral + s;
            
                                                            i++;
                                           }
    
    
                                                            cout <<"\nThe answer is:  " << integral;
    
                                                            }                                  
    
                             }
                                             
    }
                           float funcj(float x)
    
                       {
    
                          float f;
    
    
                          f = (x*x) - (5*x) - 3 ;
    
                           return f;
                       }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    What you are asking about doing is writing an expression parser... no easy task for a beginner. There may be some libraries out there that you could use or borrow from instead of writing your own.

    Some general comments:

    1. There is a "preview" button. Learn to use it before you make your post. The current formating of the code makes it difficult to read which tends to affect how willing others are to look at your code and make suggestions and find errors (reformated below).

    2. main returns an int, not a float.

    3. There are newer versions of the headers you are using. You should consider switching if your compiler supports them. If not, get a newer compiler.


    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <fstream>
    using namespace std;
    
    float funcj(float);
    
    int main()
    {
        int n, i;
        float a, b, h, x, s, integral;
                                
        i = 0;
            
        cout << "Please input a, the lower bound:  ";
        cin >> a;
        cout << "\nPlease input b, the upper bound:  ";
        cin >> b;
    
        if (a >= b)
        {
            cout << "\nYou have inputted the data incorrectly. Please try again.\n";
        }
    
        else
        {
            integral = 0;
    
            cout << "\nPlease input the number of strips required:  ";
            cin >> n;
    
            {
                                                    
                h = fabs(a - b) / n;
            
                while ( i < n)
                {
                                                            
                    x = a + i * h;
            
                    s = h * funcj(x);
                                                            
                    integral = integral + s;
            
                    i++;
                }
    
                cout <<"\nThe answer is:  " << integral;
    
            }                                  
    
        }
    
        return 0;
                                             
    }
    
    float funcj(float x)
    {
        float f;
    
        f = (x*x) - (5*x) - 3 ;
    
        return f;
    }
    Last edited by hk_mp5kpdw; 03-16-2006 at 06:12 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    ok, thanks for the advice. i think i will give the parser a miss!!

    it wasn't asked of me, i just thought it would be good to have it though, but its probably not worth it.

    i will have a quick look at it mind. just out of curiosity

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21