This is a part of the code for a assignment. it compiles properly but whenver i run it it runs fine up until where i call the index_sort function where codeblocks returns the value 255<OxFF> as error and terminates .
#include <iostream>
#include<stdlib.h>
#include<fstream>
usingnamespace std;
class stud
{ public:
string roll;
float cpi;
int cred[8];
int credsum=0;
};
void index_sort(int a[], stud rec[],int n) //to sort acc roll number
{int pass, j;
int tmp;
bool xchange = true;
for (pass = 1; pass < n && xchange == true; pass++)
{ xchange = false;
for (j=0; j < n - pass; j++)
{
if (rec[a[j]].roll > rec[a[j+1]].roll)
{ tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp;
xchange = true;
} /* end of if */
} /* end of inner for loop */
} /* end of outer for loop */
}
void index_sort1(int a[], stud rec[],int n) //To sort acc cpi
{int pass, j;
int tmp;
bool xchange = true;
for (pass = 1; pass < n && xchange == true; pass++)
{ xchange = false;
for (j=0; j < n - pass; j++)
{
if (rec[a[j]].cpi < rec[a[j+1]].cpi)
{ tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp;
xchange = true;
} /* end of if */
} /* end of inner for loop */
} /* end of outer for loop */
}
int main()
{ ifstream inp("acad_records.csv", ios::in);
stud det[50];
int totcred=252,cnt=0,a[50];
string s;
getline(inp,s);
unsignedint k = 0;
// code To input student Data from file
/* Extra work to be done acc the assignment problem ommited here for brevity
cout<<"List of students eligible for minors or honours: "<<endl;
for(int i=0;i<cnt;i++)
{ if(det[i].credsum>281)
cout<<det[i].roll<<endl;
}
*/
//program runs perfectly up until here
cout<<"Sorted list according to cpi: \n\n";
index_sort1(a,det,cnt); //error while calling this function
for(int i=0;i<cnt;i++)
{
cout<<det[a[i]].roll<<" "<<det[a[i]].cpi<<endl;
}
index_sort(a,det,cnt);
for(int i=0;i<cnt;i++)
{
cout<<det[a[i]].roll<<" "<<det[a[i]].cpi<<endl;
}
}
I wanted to mention something unrelated to the problem first:
You should really consider code indentation: http://en.wikipedia.org/wiki/Indent_style
It makes your code a lot more readable and you won't need any lines like "end of for loop".
Also, give every statement its own line, rather than writing multiple on a single line.
As for your problem - I assume you want the csv file to be loaded into the array?
However, I don't see any statement of you loading any information of your stud-array, which you are wanting to sort. As the index_sort function assumes there is content in the array, a memory error will occur when you try to access its non-existant members.
> However, I don't see any statement of you loading any information of your stud-array,
> which you are wanting to sort.
> As the index_sort function assumes there is content in the array,
> a memory error will occur when you try to access its non-existant members.
The members do exist, they have garbage.
However, sorting garbage shouldn't produce a crash.
The elements of `a' are unintialized, and you are using them to index another array. That may access out of bounds
Thanks just figured out i forgot to initialize the index array a[]. Progs working perfectly now
I had skipped the code to load in data into to student array so as to keep to code short.
Will indent properly next time.