Weird error being returned by codeblocks

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 .

Thanks for the help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include<stdlib.h>
#include<fstream>
using namespace 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);
    unsigned int 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;
}
}
closed account (o3hC5Di1)
Hi there,

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.

Hope that helps.

All the best,
NwN
http://www.cplusplus.com/forum/general/112111/

> 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.
Topic archived. No new replies allowed.