bus error (core dump)

hi i am writing a program and i got bus error. im doing as much research about it as i can i really am confused about how to go about this. basically my project so far is reading in a polynomial from a txt file, placing it into an array of structs and sorting the polynomial from smallest exponent to largest exponent. any input or help is much appriciated!!! :D thank you!


#include<iostream>
#include<fstream>
using namespace std;

struct term
{
int coefficient;
int exponent;
};

void InsertSort(ifstream &InputFile, term polynomial[], int &size);
int FindPosition(term newTerm, term polynomial[], int size);
void MoveData(int position, term polynomial[], int &size);
void OutputArray(ofstream &OutputFile, term polynomial[], int size);
void setPoly(ifstream &InputFile, term polynomial[], int &size);
void setTerm(ifstream &InputFile, term polynomial[], term &newTerm);

int main()
{

ifstream InputFile;
ofstream OutputFile;

term polynomial[100];
int size=0;

InputFile.open("polynomial.txt");
if (!InputFile)
{
cout << "Error opening inputfile!" << endl;
exit(1);
}

OutputFile.open("results.txt");

setPoly(InputFile, polynomial, size);
OutputArray(OutputFile, polynomial, size);

InputFile.close();
OutputFile.close();

return 0;
}


void InsertSort(ifstream &InputFile, term polynomial[], int &size)
{
term newTerm;
int position;

while(InputFile)
{
if (size == 0)
{
polynomial[0]=newTerm;
size++;
}
else
{
position = FindPosition(newTerm, polynomial, size);
MoveData(position, polynomial, size);
polynomial[position] = newTerm;
size ++;
}
}
}


void setPoly(ifstream &InputFile, term polynomial[], int &size)
{

term newTerm;
int next;
char nextchar;
char blank = ' ';
bool done = false;

size = 0;
while(!InputFile.eof() && next <=100 && !done)
{
setTerm(InputFile, polynomial, newTerm);

if(InputFile)
{
InsertSort(InputFile, polynomial, size);
size ++;
InputFile.get(nextchar);
while(nextchar == blank)
{
InputFile.get(nextchar);
}
done = nextchar == '\n';
}
else
{
done = true;
}
}
}


void setTerm(ifstream &InputFile, term polynomial[], term &newTerm)
{
char c;
InputFile >> newTerm.coefficient;
InputFile.get(c);
InputFile.get(c);
InputFile >> newTerm.exponent;
}

int FindPosition(term newTerm, term polynomial[], int size)
{
int index;
index = 0;
while (index < size)
{
if (newTerm.exponent > polynomial[index].exponent)
{
index++;
}
else
return(index);
}
return(size);
}

void MoveData(int position, term polynomial[], int &size)
{
int index;
for (index=size+1; index>position; index--)
{
polynomial[index] = polynomial[index-1];
}
}


void OutputArray(ofstream& OutputFile, term polynomial[], int size)
{
int i;
for(i=0; i<size; i++)
{

OutputFile << polynomial[i].coefficient << "x^" << polynomial[i].exponent << endl;
}
}
closed account (z05DSL3A)
It looks like you are using un-initialised variables, for example in setPoly() you have term newTerm; int next; but do not give them any values before using them.

PS. Please read the following for [code] tag use:
How to use tags:
http://www.cplusplus.com/articles/firedraco1/
Topic archived. No new replies allowed.