I've written a program that allows you to calculate the definite integral of a function but I feel that it is limited and I want to rewrite it so that the user inputs an equation which is then stored as a string and then that string becomes the equation for the double float variable that I want to use. I have two ideas on how it could be implemented but I don't know how to code it. One method would be for the program to go through the string and take each character in it and append it to the double float variable. Two the program could convert each character, create an array where each target is one character from the string and then these are appended to the end to the end of the double float variable. I'm a beginner so if possible could you also explain what each function means cause it's all Greek to me at this point.
This is my initial code:
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
double f1(double a, double b, double e,double d) //My function for the y values
{
double y, h;
h=(powf(a, b));
y=(e)*(h)+d; //Equation for the function
return (y);
}
int main()
{
double xmin, xmax, inc, g, coex, x, c, Sum, expox, y1; //My variables
Sum=0;
do {
cout << "What is the coefficient of x? ";
cin >> coex;
cout << "What is the exponent of x? ";
cin >> expox;
cout << "Is there a constant? ";
cin >> c;
cout << "What is the lower boundary? ";
cin >> xmin;
cout << "What is the upper boundary? ";
cin >> xmax;
cout << "In what increments do you wish to integrate? ";
cin >> inc;
xmax=xmax-inc;
x=xmin;
Sum=0;
do {
y1=fabs(f1(x, expox, coex, c)); //First y value
Sum=Sum+(y1)*inc; //The Sum of the area under the graph
x=x+inc;
} while (x<=xmax);
cout << "\n Integral=" << setprecision(12) << Sum;
cout << "\n Press 1 then enter to rerun or press 0 and enter to quit. \n";
cin >> g;
} while (g==1);
if (g==0)
{
cout << "Goodbye! I hope I've been able to help! Designed and coded by Ekin Ozturk. Please do not take credit for something you haven't done. :)";
cin.get();
}
}