ok, this is what i have. apologies for incorect formatting, i'm new to this. i'm having to go out in a few minutes so i won't reply for a while either.
Code:
#include <iostream.h>
#include <iomanip.h>
#include <cmath.h>
#include <fstream.h>
#include <limits.h>
#include <stdio.h>
#include <conio.h>
float funcj(float);
int main()
{
int n, i, m;
float a, b, h, x, s, integral;
clrscr();
cout << "************************************************************************\n\n";
textcolor (12);
cprintf ("The function you are integrating is x^2 - 5x - 3");
cout << "\n\nWould you like to input your values?\n";
cout << "OR would you like to input the values from the file rectangle.dat?";
cout << "\n\nOption 1: Input the values\n";
cout << "\nOption 2: Read the values\n";
cout << "\n\nPlease enter your choice, 1 or 2: ";
cin >> m;
if (m == 1)
{
cout << "\n\nPlease input a, the lower bound: ";
while(!(cin >> a))
{
textcolor (2);
cprintf ("You have inputted the data incorrectly.");
cout << "\nPlease try again. ";
return 0;
}
cout << "\nPlease input b, the upper bound: ";
//cin >> b;
while(!(cin >> b ))
{
textcolor (2);
cprintf ("You have inputted the data incorrectly.");
cout << "\nPlease try again. ";
return 0;
}
cout << "Please enter the number of strips: ";
while(!(cin >> n)) // If the stream fails from bad input
{
cin.clear(); // Clear the fail state
cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); // Clear the buffer
cout << "Invalid statement, please re-enter: "; // Reprompt
}
}
else if (m == 2)
{
ifstream read ("rectangle.dat");
read >> a >> b >> n;
}
else
{
textcolor (2);
cprintf ("You have inputted data incorrectly.");
cout << "\nPlease try again.";
return 0;
}
if (a >= b)
{
textcolor (2);
cprintf ("\nYou have entered the data incorrectly.");
cout << "\na must be lower than b.\n";
cout << "Please Try Again.\n";
}
// else if ( n < 1)
// {
// textcolor (2);
// cprintf ("\nYou have entered the data incorrectly.");
// cout << "\nYou must have a positive (integer) number of strips.\n";
// cout << "Please try again.";
// }
else
{
integral = 0;
h = fabs(a - b) / n;
for (i = 0; i < n; i++ )
{
x = a + i * h;
s = h * funcj(x);
integral = integral + s;
}
textcolor (4);
cprintf ("\nThe answer is: ");
cout << integral << "\n";
}
return 0;
}
float funcj(float x)
{
float f;
f = (x*x) - (5*x) - 3 ;
return f;
}
i'm aware i haven't used the std that you have, so i will alter it if you need me too when i get back.
its not the finished article by any means. if you do run the program, if u use option 2, you'll need a file called rectangle.dat
which just contains 3 numbers.
// where a =2, b = 5, n = 100]
this just shows how i can read data from a file. its not needed but i'm just showing my teacher i can read data from a file as well as from inputting the data in the prog.
its by no means finished, but if i can just check for n being an integer, then i can then tweak it, so its more user friendly.
thanks for the help. its much appreciated.
sorry for the rushed messgae.