I have to be able to read text from a file then manipulate it to find the average value of an integral over a given bounds [a,b] from an input file, then export it to another file. I do not know how to store the information to process it. Say this is the input file input.txt (each line is a different polynomial):

3 1.3 0.9 4.7 2 -5.4 5
2 5 6.3 1 0.4 10.7
1 1 0 -1 2

I would want to be able to store the bold numbers as the degree of the polynomial. The underlined numbers are the values of a,b respectively. the rest of the numbers are the coefficients.

ex) line 1 would be 3rd degree polynomial 1.3x^2+0.9x+2 integrated over [-5.4,5]

I realize if I store each line as an array of coef[], I can set
a=coef
b=coef
but I don't know how I would find the degree (bold number, first number in each line) to set it equal so that I can evaluate the polynomial to integrate etc. since the lines each have different amounts of numbers. If i can get these each set to a separate memory, then I know how to evaluate the integral and print like I need it.

Here's my code so you may see what I am doing (pardon the redundancy). Please let me know if I am unclear:

``````#include<iostream>
#include<cmath>
#include<cctype>
using namespace std;

char getChoice();
char firstChoice();
int getN();
double f(double x, double coef[], int n);
double integral(double a, double b, double coef[], int n);
void print_polynomial(double coef[], int n);

int main (void) {
char userChoice;
int n,x,i;
double a,b;
double coef;
cout << "\nThis program finds definite integral\nof polynomial on [a,b]\n\n";
cout << "0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n4. Process batch\n\nEnter your choice : ";
userChoice = firstChoice();
do{
switch(userChoice)
{
case '0': //ends program
return 0;

case '1': cout << "Enter degree of a polynom between 2 and 10 : ";
n=getN();
cout << "Enter space separated coefficients starting from highest degree\n";
for(i=n;i>=0;i--)
cin>>coef[i];
break;

case '2':  print_polynomial(coef,n);
cout<<endl;
break;

case '3': integral(a, b, coef, n);
cout<<endl;
break;

case '4'://This case is the area in which the input file would be read,
//processed, and outputted.

cout<<endl;
break;
}
cout << "0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n4. Process batch\n\nEnter your choice : ";
userChoice=getChoice();
}while(true);
}

//Prototypes

char firstChoice()
{
char choice;
cin >> choice;
while(isalpha(choice))
{
cout << "'" << choice << "' is an invalid choice\nEnter your choice : ";
cin >> choice;
}

if (choice == '0' || choice == '1' || choice == '4')
{
return choice;
}
while(choice!= '0' && choice != '1' && choice != '4')
{
if (choice == '2' || choice == '3')
{
cout << "Enter the polynomial first\n0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n4. Process batch\n\nEnter your choice : ";
cin >> choice;
}
if (choice != '0' && choice != '1' && choice != '2' && choice != '3' && choice != '4')
{
cout << "'" << choice << "' is an invalid choice\nEnter your choice : ";
cin >> choice;
}
}

return choice;
}

char getChoice()
{
char choice;
cin >> choice;

while(isalpha(choice))
{
cout << "'" << choice << "' is an invalid choice\nEnter your choice : ";
cin >> choice;
}

while (choice != '0' && choice != '1' && choice != '2' && choice != '3' && choice != '4')
{
cout << "'" << choice << "' is an invalid choice\nEnter your choice : ";
cin >> choice;
}
return choice;
}

int getN()
{
int degree;

cin >> degree;

while (degree < 2 || degree > 10)
{
cout << "Enter degree of a polynom between 2 and 10 : ";
cin >> degree;
}
return degree;
}
double f(double x, double coef[], int n)
{
double hold;
double sum=0;
cout << "x : ";
cin >> x;
for(int i=n;i>=1;i--)
{
hold=sum;
sum=coef[i]*pow(x,i);
sum+=hold;
}
sum=sum+coef;
cout << sum;

}
double integral(double a, double b, double coef[], int n)
{
double sum1=0;
double sum2=0;
double finalSum;
double hold1,hold2;

cout << "Enter integration limits a b : ";
cin>>a>>b;

for(int i=n;i>0;i--)
{
hold1=sum1;
sum1=coef[i]*(pow(a,i+1)/(i+1));
sum1=sum1+hold1;
}
sum1=sum1+coef*a;

for(int i=n;i>0;i--)
{
hold2=sum2;
sum2=coef[i]*(pow(b,i+1)/(i+1));
sum2=sum2+hold2;
}
sum2=sum2+coef*b;
finalSum=sum2-sum1;
cout << "Integral of f(x) equals to " << finalSum;
}
void print_polynomial(double coef[], int n)
{
if(coef[n]==1) cout << "x^"<<n;
if(coef[n]==-1) cout<< "-x^"<<n;
if(coef[n]>0 && coef[n]!=1) cout << coef[n]<<"*x^"<<n;
if(coef[n]<0 && coef[n]!=-1) cout << coef[n]<<"*x^"<<n;

for(int i=n-1;i>=2;i--)
{
if(coef[i]>0) cout<<"+";
if(coef[i]==1) cout<<"x^"<<i;
if(coef[i]==-1) cout<<"-x^"<<i;
if(coef[i]<0 && coef[i]!=-1) cout<<coef[i]<<"*x^"<<i;
if(coef[i]>0 && coef[i] !=1) cout<<coef[i]<<"*x^"<<i;
}
if(coef==1) cout <<"+x";
if(coef==-1) cout<<"-x";
if(coef>0 && coef !=1) cout<<"+"<<coef<<"*x";
if(coef<0 && coef !=-1) cout<<coef<<"*x";

if(coef>0) cout<<"+"<<coef;
if(coef<0) cout<<coef;
}``````

You can use the degree of the polynomial to allocate an array of size degree+1 (to hold the constant) for each line. Your degree will let you read ahead to see how many times you should go through a loop collecting coefficients and then just grab the last 2 values.

There's nothing to delete in your code yet. However, you haven't specified a length for your array. Coeff should be a double * so you can have a line between 28 and 29, `double * coeff = new double[n+1];` (since your polynomial has n+1 terms). Then at the end you …

``````char input;
char output;

cout<<"Enter input file name : ";
cin >> input;
cout<<"Enter output file name : ";
cin >> output;
while(!in.eof())    // in is not even open, it can't be at EOF yet
{
in.open(input);   // Open in
while(false)
{
if(in.fail()) // If in is …``````
``````while(!in.eof())
{
in>>n;
double * coeff = new double[n+1];
for(int i=n+1;i>=0;i--)
cin>>coef[i];
in>>a>>b;``````
``````while(in >> n) //don't use .eof see "http://www.daniweb.com/forums/post155265-18.html"
{
// so now we've got n …``````

One thing to note, you have had there a simple out-of-bounds write

``````// 'n + 1' elements -> last valid index is 'n', not n + 1
double * coeff = new double[ n + 1 ];

for(int i= n + 1 ;i>=0;i--)
// here it would …``````

## All 25 Replies

Pending that I can use the >> operator to assign a single line to an array of coef[]...

If I input each line separately as an array, say coef (since 12 would be the max amount of values that we are required to have), how would I find the very first value of that array (coef[max]) since the array can be any value between coef and coef?

Also, how do I separate each line of data, say I had three lines but I wanted to input only one line to process it completely, then move to the next?

You can use the degree of the polynomial to allocate an array of size degree+1 (to hold the constant) for each line. Your degree will let you read ahead to see how many times you should go through a loop collecting coefficients and then just grab the last 2 values.
3 1.3 0.9 4.7 2 -5.4 5
2) Create a matrix of size 4
3) Have a loop that reads in just 1 coefficient value, repeat it the degree of the poly + 1
4) Read the last 2 values for the bounds
5) Do the integral
6) Delete the array

Thanks, jonsca. That makes a lot of sense. I'm going to the library this evening to try to get all my work done so I'll work on implementing it. Doesn't seem too difficult, but I probably shouldn't speak too soon. I'll reply back here if there's any more questions. Thanks again

I really appreciate the help jonsca. This site really helps relieve some stress :sweat: so I've decided to donate a years worth. I feel that it has a lot to offer me and well worth the money. Just wanted to let you know that your help motivated my actions. But on topic:

I spent a few minutes today and tried to do what you had suggested. I created this within a prototype function to do what I need to for easy implementing within the main function. I'm about to head to work but I wanted to see if I'm going along the right path/a little proof reading.

What I haven't done :
1) Find a way to delete array for each separate line of data
2)Close files
3)More things that I can't think of now( haha)

``````process(ifstream& in, ofstream& out)
{
ifstream in;
ofstream out;
int n;
double a,b,coef[],average;
double sum1=0;
double sum2=0;
double finalSum;
double hold1,hold2;

cout<<"Enter input file name : ";
cin>>input;
cout<<"enter output file name : ";
cin>>output;

in.open(input);
while(false)
{
if(in.fail())
{
cout<<"input file did not open please check it\n";
return false;
}
}
out.open(output);

in>>n;
for(i=n+1;i>=0;i--)
cin>>coef[i];
in>>a>>b;

out<<"Average of ";
if(coef[n]==1) out << "x^"<<n;
if(coef[n]==-1) out<< "-x^"<<n;
if(coef[n]>0 && coef[n]!=1) out << coef[n]<<"*x^"<<n;
if(coef[n]<0 && coef[n]!=-1) out << coef[n]<<"*x^"<<n;

for(int i=n-1;i>=2;i--)
{
if(coef[i]>0) out<<"+";
if(coef[i]==1) out<<"x^"<<i;
if(coef[i]==-1) out<<"-x^"<<i;
if(coef[i]<0 && coef[i]!=-1) out<<coef[i]<<"*x^"<<i;
if(coef[i]>0 && coef[i] !=1) out<<coef[i]<<"*x^"<<i;
}
if(coef==1) out <<"+x";
if(coef==-1) out<<"-x";
if(coef>0 && oef !=1) cout<<"+"<<coef<<"*x";
if(coef<0 && oef !=-1) cout<<coef<<"*x";

if(coef>0) out<<"+"<<coef;
if(coef<0) out<<coef;

out<<", on ["<<a<<","<<b<<"] is ";

for(int i=n;i>0;i--)
{
hold1=sum1;
sum1=coef[i]*(pow(a,i+1)/(i+1));
sum1=sum1+hold1;
}
sum1=sum1+coef*a;

for(int i=n;i>0;i--)
{
hold2=sum2;
sum2=coef[i]*(pow(b,i+1)/(i+1));
sum2=sum2+hold2;
}
sum2=sum2+coef*b;
finalSum=sum2-sum1;
average=finalSum/(b-a);
out<<average;``````

Here's some sample input's and outputs from the .exe file my class has provided in it's website (I can offer this if it makes it easier to help)
sample input :

``````3 1.3 0.9 4.7 2 -5.4 5
2 5 6.3 1 0.4 10.7
1 1 0 -1 2``````

sample output :

Average of 1.3*x^3+0.9*x^2+4.7*x+2, on [-5.4,5] is 2.1672
Average of 5*x^2+6.3*x+1, on [0.4,10.7] is 234.182

NOTE: I'm not sure why, but the output only has two lines whereas the input has 3 lines? This is from the provided .exe not mine, so ultimately it isn't a big deal.

There's nothing to delete in your code yet. However, you haven't specified a length for your array. Coeff should be a double * so you can have a line between 28 and 29, `double * coeff = new double[n+1];` (since your polynomial has n+1 terms). Then at the end you call `delete [] coeff;`

Having a little trouble with configuring the code so that the user can input the name of the text input and output files. Here's what I have now as far as cout/cin for the input files and the declarations :

``````char input;
char output;

cout<<"Enter input file name : ";
cin >> input;
cout<<"Enter output file name : ";
cin >> output;
while(!in.eof())
{
in.open(input);
while(false)
{
if(in.fail())
{
cout<<"input file did not open please check it\n";
return false;
}
}
out.open(output);``````
``````char input;
char output;

cout<<"Enter input file name : ";
cin >> input;
cout<<"Enter output file name : ";
cin >> output;
while(!in.eof())    // in is not even open, it can't be at EOF yet
{
in.open(input);   // Open in
while(false)
{
if(in.fail()) // If in is in failure state -- but nothing
// is ever done with in to make it fail
{           // except open.
cout<<"input file did not open please check it\n";
return false;
}
}
out.open(output);``````

I've been looking at this (and numbers) too much recent, everything seems the same...I've done a little revamp on the whole opening scenario from a little help from the lecture slides (who would have thought).

Main areas of thought :
1) implementation of prototype double process(in,out)---is this correct?
2)the prototype double process(instream& in, ofstream& out) as a whole, however mainly on the ability to accept a file name

``````#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<fstream>
using namespace std;

char getChoice();
char firstChoice();
int getN();
double f(double x, double coef[], int n);
double integral(double a, double b, double coef[], int n);
void print_polynomial(double coef[], int n);
double process(ifstream& in, ofstream& out);

int main (void) {
char userChoice;
ifstream in;
ofstream out; //is this needed?
int n,x,i,degree;
double a,b;
double coef;
cout << "\nCGS 2421 Integrals of polynomials\nThis program finds definite integral\nof polynomial on [a,b]\n\n";
cout << "0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n4. Process batch\n\nEnter your choice : ";
userChoice = firstChoice();
do{
switch(userChoice)
{
case '0': //ends program
return 0;

case '1': cout << "Enter degree of a polynom between 2 and 10 : ";
n=getN();
cout << "Enter space separated coefficients starting from highest degree\n";
for(i=n;i>=0;i--)
cin>>coef[i];
break;

case '2':  print_polynomial(coef,n);
cout<<endl;
break;

case '3': integral(a, b, coef, n);
cout<<endl;
break;

case '4': process(in,out);

//This case is the area in which the input file would be read,
//processed, and outputted.

cout<<endl;
break;
}
cout << "0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n4. Process batch\n\nEnter your choice : ";
userChoice=getChoice();
}while(true);
}

//Prototypes

char firstChoice()
{
char choice;
cin >> choice;
while(isalpha(choice))
{
cout << "'" << choice << "' is an invalid choice\nEnter your choice : ";
cin >> choice;
}

if (choice == '0' || choice == '1' || choice == '4')
{
return choice;
}
while(choice!= '0' && choice != '1' && choice != '4')
{
if (choice == '2' || choice == '3')
{
cout << "Enter the polynomial first\n0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n4. Process batch\n\nEnter your choice : ";
cin >> choice;
}
if (choice != '0' && choice != '1' && choice != '2' && choice != '3' && choice != '4')
{
cout << "'" << choice << "' is an invalid choice\nEnter your choice : ";
cin >> choice;
}
}

return choice;
}

char getChoice()
{
char choice;
cin >> choice;

while(isalpha(choice))
{
cout << "'" << choice << "' is an invalid choice\nEnter your choice : ";
cin >> choice;
}

while (choice != '0' && choice != '1' && choice != '2' && choice != '3' && choice != '4')
{
cout << "'" << choice << "' is an invalid choice\nEnter your choice : ";
cin >> choice;
}
return choice;
}

int getN()
{
int degree;

cin >> degree;

while (degree < 2 || degree > 10)
{
cout << "Enter degree of a polynom between 2 and 10 : ";
cin >> degree;
}
return degree;
}
double f(double x, double coef[], int n)
{
double hold;
double sum=0;
cout << "x : ";
cin >> x;
for(int i=n;i>=1;i--)
{
hold=sum;
sum=coef[i]*pow(x,i);
sum+=hold;
}
sum=sum+coef;
cout << sum;

}
double integral(double a, double b, double coef[], int n)
{
double sum1=0;
double sum2=0;
double finalSum;
double hold1,hold2;

cout << "Enter integration limits a b : ";
cin>>a>>b;

for(int i=n;i>0;i--)
{
hold1=sum1;
sum1=coef[i]*(pow(a,i+1)/(i+1));
sum1=sum1+hold1;
}
sum1=sum1+coef*a;

for(int i=n;i>0;i--)
{
hold2=sum2;
sum2=coef[i]*(pow(b,i+1)/(i+1));
sum2=sum2+hold2;
}
sum2=sum2+coef*b;
finalSum=sum2-sum1;
cout << "Integral of f(x) equals to " << finalSum;
}
void print_polynomial(double coef[], int n)
{
if(coef[n]==1) cout << "x^"<<n;
if(coef[n]==-1) cout<< "-x^"<<n;
if(coef[n]>0 && coef[n]!=1) cout << coef[n]<<"*x^"<<n;
if(coef[n]<0 && coef[n]!=-1) cout << coef[n]<<"*x^"<<n;

for(int i=n-1;i>=2;i--)
{
if(coef[i]>0) cout<<"+";
if(coef[i]==1) cout<<"x^"<<i;
if(coef[i]==-1) cout<<"-x^"<<i;
if(coef[i]<0 && coef[i]!=-1) cout<<coef[i]<<"*x^"<<i;
if(coef[i]>0 && coef[i] !=1) cout<<coef[i]<<"*x^"<<i;
}
if(coef==1) cout <<"+x";
if(coef==-1) cout<<"-x";
if(coef>0 && coef !=1) cout<<"+"<<coef<<"*x";
if(coef<0 && coef !=-1) cout<<coef<<"*x";

if(coef>0) cout<<"+"<<coef;
if(coef<0) cout<<coef;
}

double process(ifstream& in, ofstream& out)
{

int n;
double a,b,coef[n+1],average;
double sum1=0;
double sum2=0;
double finalSum;
double hold1,hold2;
bool done=false;
char fname ;
char fname2 ;

do { // until open is successful
cout << "Enter input file name : ";
cin.getline(fname, sizeof(fname));
in.open(fname);

if (in.fail()) {
cout<<"Open error!\nTry again.\n"<<endl;
in.clear();
}
else done = true;
} while (!done);

do {	         // until open is successful
cout << "Enter output file name : ";
cin.getline(fname2, sizeof(fname2));
out.open(fname2);

if (out.fail()) {
cout<<"Open error!\nTry again.\n"<<endl;
out.clear();
}
else done = true;
} while (!done);

while(!in.eof())
{
in>>n;
double * coeff = new double[n+1];
for(int i=n+1;i>=0;i--)
cin>>coef[i];
in>>a>>b;

out<<"Average of ";
if(coef[n]==1) out << "x^"<<n;
if(coef[n]==-1) out<< "-x^"<<n;
if(coef[n]>0 && coef[n]!=1) out << coef[n]<<"*x^"<<n;
if(coef[n]<0 && coef[n]!=-1) out << coef[n]<<"*x^"<<n;

for(int i=n-1;i>=2;i--)
{
if(coef[i]>0) out<<"+";
if(coef[i]==1) out<<"x^"<<i;
if(coef[i]==-1) out<<"-x^"<<i;
if(coef[i]<0 && coef[i]!=-1) out<<coef[i]<<"*x^"<<i;
if(coef[i]>0 && coef[i] !=1) out<<coef[i]<<"*x^"<<i;
}
if(coef==1) out <<"+x";
if(coef==-1) out<<"-x";
if(coef>0 && coef !=1) cout<<"+"<<coef<<"*x";
if(coef<0 && coef !=-1) cout<<coef<<"*x";

if(coef>0) out<<"+"<<coef;
if(coef<0) out<<coef;

out<<", on ["<<a<<","<<b<<"] is ";

for(int i=n;i>0;i--)
{
hold1=sum1;
sum1=coef[i]*(pow(a,i+1)/(i+1));
sum1=sum1+hold1;
}
sum1=sum1+coef*a;

for(int i=n;i>0;i--)
{
hold2=sum2;
sum2=coef[i]*(pow(b,i+1)/(i+1));
sum2=sum2+hold2;
}
sum2=sum2+coef*b;
finalSum=sum2-sum1;
average=finalSum/(b-a);
out<<average;

delete [] coeff;
}
}``````

I guess code tags don't work with other color...sorry if anyone doesn't like this. I wanted the color to stress my areas of concern. Weak attempt to "ease" the readability of this post

``````while(!in.eof())
{
in>>n;
double * coeff = new double[n+1];
for(int i=n+1;i>=0;i--)
cin>>coef[i];
in>>a>>b;``````
``````while(in >> n) //don't use .eof see "http://www.daniweb.com/forums/post155265-18.html"
{
// so now we've got n do the allocation
//instead of cin you need to use in
for(int i = n+1;i>=0;i--)
in >>coeff[n];      //we get the exact number of coeff we need
in >>a >>b;
//do the processing
//output results
delete [] coeff;
}``````

Then your loop is all ready for the next line, it get's the next n, allocates again, does the processing you're in business.
(otherwise reading in all of the data at the same time would give you a 2d array with varying lengths in one of the dimensions which could get messy).

I appreciate the help and explanations. .eof was emphasized pretty widely in the class, so I figured it made sense here, but that link you posted couldn't be more applicable.

I tried compiling and it went through, but every time i run it and try option 4, it crashes. Is double the right declaration for process(ifstream& in, ofstream& out) ?? Not sure how to prototype this, but I feel like the body of this prototype accomplishes what I want to do.

Well, you're not needing any return value back in main() nor are you returning anything in the method so really you should use void.

Try to figure out where it's crashing. Either run it through a debugger or pepper your code with couts to watch its progress.

One thing to note, you have had there a simple out-of-bounds write

``````// 'n + 1' elements -> last valid index is 'n', not n + 1
double * coeff = new double[ n + 1 ];

for(int i= n + 1 ;i>=0;i--)
// here it would go wrong when i == n + 1 ...
cin>>coef[i];``````
commented: Good catch. +2

I was able to solve my crash issue, (I think the opening of in/out works now), but I can't get any output in my output.txt file that I use.
Also, I wanted to avoid pointers if at all possible since we're just learning them this week, so I tried taking note of the n+1 factors and implementing it. I must not be inputting or outputting correctly. Here is my updated method with changes (note I found that we should use process as an int) :

``````int process(ifstream& in, ofstream& out)
{

int n;
double a,b,coef,average;
double sum1=0;
double sum2=0;
double hold1,hold2,finalSum;
bool done=false;
char fname;
char fname2;

do { // until open is successful
cout << "Enter input file name : ";
cin.getline(fname, sizeof(fname));
in.open(fname);

if (in.fail()) {
cout<<"Open error!\nTry again.\n"<<endl;
in.clear();
}
else done = true;
} while (!done);
done=false;
do {	         // until open is successful
cout << "Enter output file name : ";
cin.getline(fname2, sizeof(fname2));
out.open(fname2);

if (out.fail()) {
cout<<"Open error!\nTry again.\n"<<endl;
out.clear();
}
else done = true;
} while (!done);

while(in>>n)
{
in>>n;
for(int i=n+1;i>=0;i--)
in>>coef[i];
in>>a>>b;
n=n+1;

out<<"Average of ";
if(coef[n]==1) out << "x^" <<n;
if(coef[n]==-1) out << "-x^" <<n;
if(coef[n]>0 && coef[n]!=1) out << coef[n]<<"*x^"<<n;
if(coef[n]<0 && coef[n]!=-1) out << coef[n]<<"*x^"<<n;

for(int i=n-1;i>=2;i--)
{
if(coef[i]>0) out<<"+";
if(coef[i]==1) out<<"x^"<<i;
if(coef[i]==-1) out<<"-x^"<<i;
if(coef[i]<0 && coef[i]!=-1) out<<coef[i]<<"*x^"<<i;
if(coef[i]>0 && coef[i] !=1) out<<coef[i]<<"*x^"<<i;
}
if(coef==1) out <<"+x";
if(coef==-1) out<<"-x";
if(coef>0 && coef !=1) out<<"+"<<coef<<"*x";
if(coef<0 && coef !=-1) out<<coef<<"*x";

if(coef>0) out<<"+"<<coef;
if(coef<0) out<<coef;

out<<", on ["<<a<<","<<b<<"] is ";

for(int i=n;i>0;i--)
{
hold1=sum1;
sum1=coef[i]*(pow(a,i+1)/(i+1));
sum1=sum1+hold1;
}
sum1=sum1+coef*a;

for(int i=n;i>0;i--)
{
hold2=sum2;
sum2=coef[i]*(pow(b,i+1)/(i+1));
sum2=sum2+hold2;
}
sum2=sum2+coef*b;
finalSum=sum2-sum1;
average=finalSum/(b-a);
out<<average;

for(int i=n;i>=0;i--)
coef[i]=0;
n=0;
a=0;
b=0;
done=false;
}
}``````

Also one small issue, when I first run process(in,out), instead of asking me for the input file, it says "Open Error\nTry again." then asks for input file again. Input file loop is messing up---I can take a look at this later though.

I get the following in the output file:

``````Average of 0.3*x^2+0.9*x+4.7, on [2,-5.4] is 5.406Average of 5*x^3+6.3*x^2+x+0.4
, on [10.7,1] is 1964.41Average of -x^1-x+2, on [0,0] is -1.#INF``````

I can't reproduce any of the errors that you have stated. It prompted me for a file name it did not skip over that. This is what I had in main():

``````ifstream in;
ofstream out;
process(in,out);``````

A couple of other things. Rather than doing all the file operations in the function I'm assuming your instructor wants you to perform all the opening in main and then just pass in the ifstream and ofstream objects. Otherwise there's no point in passing them in since you start fresh in the function anyway. Also, your function is supposed to return an int but it doesn't seem like you're sure of what that int should be -- is it a success or failure indicator or something completely different?

Also, I had wanted to say it's really cool that you decided to join the site as a sponsor. I'm confident that it means a lot to Dani too. :)

Also, I had wanted to say it's really cool that you decided to join the site as a sponsor. I'm confident that it means a lot to Dani too. :)

I was raised and still believe that when someone helps you, you should thank them. I appreciate this community and it's ability to help others for a common goal so a little donation is worth more than it costs.

I'm about to head off for another job trip in a few minutes so I don't have time to play around with the code right now, but the first due date is tonight at 11:59 so you know I'll be on later. I'll reply back with any more updates/issues.

I've found a way to open the input and output files within the main() function. I put the process(in,out) in a separate file like you did and did not get the same error, but when I went back to my original code, I got the error again. It says :

Enter input file name : Open error!
Try again.

Ente rinput file name : (it works from here)

I am having problem with the input of the coefficients. I tried a simple input file of this :

3 1 2 3 4 1 1
2 1 2 3 1 1
1 1 2 1 1

and got the output of this:

Average of 3*x^1+3*x+4, on [1,1] is NaN
Average of 3*x^1+3*x+1, on [1,1] is NaN
Average of x^2, on [0,0] is NaN

Here is what it should look like :

Average of x^3 + 2*x^2 + 3*x + 4, on [1,1] is ....
Average of x^2 + 2x + 3, on [1,1] is ...
Average of x + 2, on [1,1] is ...

where ... is solved for average integral (I can do this part)

Ideally I want to be able to finish this tonight so I appreciate any help anyone can give me! I'll be working on it until 11:59 (after this I automatically lose 20% :() Thanks!

Also, Here's my process method

``````int process(ifstream& in, ofstream& out)
{

int n;
double a,b,coef,average;
double sum1=0;
double sum2=0;
double hold1,hold2,finalSum;

while(in>>n)
{
in>>n;
for(int i=n+1;i>=0;i--)
in>>coef[i];
in>>a>>b;

out<<"Average of ";
if(coef[n]==1) out << "x^" <<n;
if(coef[n]==-1) out << "-x^" <<n;
if(coef[n]>0 && coef[n]!=1) out << coef[n]<<"*x^"<<n;
if(coef[n]<0 && coef[n]!=-1) out << coef[n]<<"*x^"<<n;

for(int i=n-1;i>=2;i--)
{
if(coef[i]>0) out<<"+";
if(coef[i]==1) out<<"x^"<<i;
if(coef[i]==-1) out<<"-x^"<<i;
if(coef[i]<0 && coef[i]!=-1) out<<coef[i]<<"*x^"<<i;
if(coef[i]>0 && coef[i] !=1) out<<coef[i]<<"*x^"<<i;
}
if(coef==1) out <<"+x";
if(coef==-1) out<<"-x";
if(coef>0 && coef !=1) out<<"+"<<coef<<"*x";
if(coef<0 && coef !=-1) out<<coef<<"*x";

if(coef>0) out<<"+"<<coef;
if(coef<0) out<<coef;

out<<", on ["<<a<<","<<b<<"] is ";

for(int i=n;i>0;i--)
{
hold1=sum1;
sum1=coef[i]*(pow(a,i+1)/(i+1));
sum1=sum1+hold1;
}
sum1=sum1+coef*a;

for(int i=n;i>0;i--)
{
hold2=sum2;
sum2=coef[i]*(pow(b,i+1)/(i+1));
sum2=sum2+hold2;
}
sum2=sum2+coef*b;
finalSum=sum2-sum1;
average=finalSum/(b-a);
out<<average;

for(int i=n;i>=0;i--)
coef[i]=0;
n=0;
a=0;
b=0;
out<<endl;
}
}``````

And here's what I have in main()

``````case '4': do { // until open is successful
cout << "Enter input file name : ";
cin.getline(fname, sizeof(fname));
in.open(fname);

if (in.fail()) {
cout<<"Open error!\nTry again.\n"<<endl;
in.clear();
}
else done = true;
} while (!done);
done=false;
do {	         // until open is successful
cout << "Enter output file name : ";
cin.getline(fname2, sizeof(fname2));
out.open(fname2);

if (out.fail()) {
cout<<"Open error!\nTry again.\n"<<endl;
out.clear();
}
else done = true;
} while (!done);

process(in,out);

cout<<endl;
break;``````

Did you try outputting each and every value read from the file to see if something is wrong in your loop? And are you sure the `coeff` array is being loaded properly? output it after the loop.

The loop looks suspicious to me.

Did you try outputting each and every value read from the file to see if something is wrong in your loop? And are you sure the `coeff` array is being loaded properly? output it after the loop.

The loop looks suspicious to me.

What do you mean by suspicious?

Sorry bear with me. I've been staring at this code for near 20 hours this week in addition to a combined 60 hours of calc 3, statics, and chem 2. It all is blending together...

I know it's not inputting the value correctly, but I'm failing to understand the mistake that it's making. It looks right to me, but I know it's not. I tried doing a cout<<coef[n] after the in>>a>>b, and that output 331, which should be 111

What do you mean by suspicious?

Sorry bear with me. I've been staring at this code for near 20 hours this week in addition to a combined 60 hours of calc 3, statics, and chem 2. It all is blending together...

I know it's not inputting the value correctly, but I'm failing to understand the mistake that it's making. It looks right to me, but I know it's not.

And with this information, what can we say? Doesn't anyone that needs help understand the concept of details?

If you know it's not inputting properly, why didn't you mention that? Why didn't you mention what it is inputting? (contents of all the variables being input). Follow that by what you expected it to input (expected contents of all the variables)

Details are the only thing that helps, not vague descriptions. Save yourself another 4 hours of pain and loss of sleep.

And with this information, what can we say? Doesn't anyone that needs help understand the concept of details?

If you know it's not inputting properly, why didn't you mention that? Why didn't you mention what it is inputting? (contents of all the variables being input). Follow that by what you expected it to input (expected contents of all the variables)

Details are the only thing that helps, not vague descriptions. Save yourself another 4 hours of pain and loss of sleep.

I apologize for this. I've honestly been trying to explain where I'm at and what I'm having trouble with and providing my code for what I've done.

From your post, I'll update my issue to clarify.

I know it is inputting. I think that somewhere the value after the degree is missing (look at the example I supplied two posts up) and from there, the coefficients are being inputted improperly. I believe the area of concern is the for(int i=n+1;i>=0;i--) loop after the in>>n.

I apologize for this. I've honestly been trying to explain where I'm at and what I'm having trouble with and providing my code for what I've done.

From your post, I'll update my issue to clarify.

I know it is inputting. I think that somewhere the value after the degree is missing (look at the example I supplied two posts up) and from there, the coefficients are being inputted improperly. I believe the area of concern is the for(int i=n+1;i>=0;i--) loop after the in>>n.

And you call this details? These are thoughts and guesses. "I believe" is a guess. "I think that somewhere" is a guess. There is not one detail in this post. Stop trying to explain. Show us.

Why didn't you mention what it is inputting? (contents of all the variables being input). Follow that by what you expected it to input (expected contents of all the variables)

Where is this information?

Did you try outputting each and every value read from the file to see if something is wrong in your loop?

Where did you give us this information?

And are you sure the `coeff` array is being loaded properly? output it after the loop.

And I don't see where this info is given either.

WaltP- I did state

Why didn't you mention what it is inputting? (contents of all the variables being input). Follow that by what you expected it to input (expected contents of all the variab

and

And are you sure the coeff array is being loaded properly? output it after the loop.

in previous posts. But I do realize I can be a little sloppy with my questions, so I will work to state things in a way that people can help easier.

On topic: I've figured out my issue. I had been using the variable "n" in both the main function and process functions. This was confusing the input. I changed "n" to "p" in process, and it works perfectly. I realized this through jonsca's idea to play with the function in a different main function.

Final Issue (see code below): When selecting option 4, it automatically reads in the '4' character as the

``````cin.getline(fname, sizeof(fname));
in.open(fname);``````

section. What I can't figure out is how to offset the '4' character so that the program doesn't immediately read in '4' and view it as an input for the file name. Any suggestions?

``````int main (void) {
char userChoice;
ifstream in;
ofstream out;
int n,x,i,degree,j;
bool done=false;
char fname;
char fname2;
double a,b;
double coef;
cout << "\nCGS 2421 Integrals of polynomials\nThis program finds definite integral\nof polynomial on [a,b]\n\n";
cout << "0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n4. Process batch\n\nEnter your choice : ";
userChoice = firstChoice();
do{
switch(userChoice)
{
case '0': //ends program
return 0;

case '1': cout << "Enter degree of a polynom between 2 and 10 : ";
n=getN();
cout << "Enter space separated coefficients starting from highest degree\n";
for(i=n;i>=0;i--)
cin>>coef[i];
break;

case '2':  print_polynomial(coef,n);
cout<<endl;
break;

case '3': integral(a, b, coef, n);
cout<<endl;
break;

case '4': do {
cout << "Enter input file name : ";
cin.getline(fname, sizeof(fname));
in.open(fname);

if (in.fail()) {
in.clear();
}
else done = true;
} while (!done);
done=false;
do {
cout << "Enter output file name : ";
cin.getline(fname2, sizeof(fname2));
out.open(fname2);

if (out.fail()) {
out.clear();
}
else done = true;
} while (!done);

process(in,out);

cout<<endl;
break;
}
cout << "0. Quit\n1. Enter polynomial\n2. Print polynomial\n3. Integrate\n4. Process batch\n\nEnter your choice : ";
userChoice=getChoice();
}while(true);
}``````

You probably need a cin.ignore() after line 13 to sop up the extra '\n' but I can't see firstChoice() to verify that.
When you take in a character and press enter the newline hangs around in the buffer and gets taken in by the next function looking for input (your getline() in choice 4). getline() reads the '\n' which is its delimiter and finishes all without getting a filename.

Ahh I see. I knew it was inputting something in place, but that explains it. Problem solved. Btw, I got lucky for this one and the deadline got pushed back till this Wednesday. All done now so one less thing to worry about. Thanks a lot.

Be a part of the DaniWeb community

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