I am trying to automate my program for Simpsons rule. Finally got it running thx to here, so now I'm trying to have it ask the user for all the variables on run. Everything is good except for inputting the function to integrate itself. After entering the function i get

Enter your function: x
Enter your function: Enter your function: Enter your function: Enter your function: Enter your function: Enter your function: Enter your function: Enter your function: -1.85119262699e+062
Press any key to continue . . .

// hw2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdio>
using namespace std;

double f_int(double x);
double I_simpson(double a, double b, int n);

int main()
{
  double a;
  double b;
  double n;
  double f;
  cout << "Enter lower limit: ";
  cin >> a;
  cout << "Enter upper limit: ";
  cin >> b;
  cout << "Enter number of partitions: ";
  cin >> n;
  
  double d = I_simpson(a, b, n);
  std::cout << std::setprecision (12) << d << std::endl;
  
system ("PAUSE");
  return 0;

}

double f_int(double x)
{
  double c;
  cout << "Enter your function: ";
  cin >> c;
	return c; 
}

double I_simpson(double a, double b, int n)
{
	double h = (b-a)/n;
	double I_simpson = 0;
	int i;
	I_simpson = (f_int(a)/6) + (f_int(b)/6);
	for ( i=1 ; i<=(n-1) ; i++)
		{
		I_simpson = I_simpson + (f_int(a + (i * h))/3);
	}
	for (i=1 ; i<=n ; i++)
		{ 
		I_simpson = I_simpson + ((2*f_int(a + (i - (1.0/2))*h))/3);
	}
	I_simpson *= h;
	return I_simpson;
}

Firstly f_int takes a double for a parameter and x is a character.

Beyond that, though, when you read the value into the function f_int doesn't suddenly become that function. Each time you use that function name in I_simpson that method will be called again and you will be reprompted. The '\n' left over in the stream from x + [enter] cascades through all of the prompts since they all are supposed to take a double too.

All that being said, this is not a trivial thing to do. One way to do it would be to hard code your (mathematical) function into a (C++) function which I think is sort of what you trying to do above. You can also look into function pointers but you'd still have to worry about translating your input into something the program can evaluate (if you want anything beyond a polynomial). If all you need is polys then look around on this site for stuff on Horner's method you could incorporate that pretty easily.

right...yea this was my first program. So i dont know about function pointers, thought it was something simple. Thanks.

thought it was something simple

No. It should be but it's one of those things.
Look around at other integration programs that you might run across on the net and see how they do it. It's something people used to get around with #defines I think.
Otherwise for the time being just have f_int as

double f_int(double x)
{
     return x;
}

for when you need f(x) = x and (replace it completely with)

double f_int(double x)
{
     return x*x;
}

when you need x^2, etc.

Hopefully someone more steeped than myself in numerical methods will have a better suggestion for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.