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);
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++)
{
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/