Switching from > to < creates gibberish error?

Hi there, trying to do an array sort and I find that when I switch from > to < on my if statement, the program breaks and generates an error. I'm sure it's something stupid I'm missing, but I thought that the sort function would be the same regardless of which way (ascending/descending) that I'm sorting. Any help is much appreciated!

Jade

Error Message:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

2012  Ä´¬´pñ࿬´Ä´Ä´¬´ÿÿØ
á¿Út·|á¿}ãK·ôÿt©£
        á¿á¿á¿Ãá¿Öá¿ãá¿
á¿#á¿Pá¿bá¿sá¿á¿á¿¥á¿µá¿êá¿ Ds·!@s·ÿûë¿dPs·
                                               º
ºº{á¿i686*** glibc detected *** ./a.out: free(): invalid pointer: 0xbfe10948 ***
======= Backtrace: =========
/lib/i686/cmov/libc.so.6[0xb7516764]
/lib/i686/cmov/libc.so.6(cfree+0x96)[0xb7518966]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb76ef2e1]
/usr/lib/libstdc++.so.6(_ZNSs4_Rep10_M_destroyERKSaIcE+0x1d)[0xb76cbb2d]
/usr/lib/libstdc++.so.6(_ZNSsD1Ev+0x51)[0xb76cd501]
./a.out[0x804b266]
./a.out[0x804aeec]
/lib/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb74be455]
./a.out(__gxx_personality_v0+0x75)[0x8048fb1]
======= Memory map: ========
08048000-0804c000 r-xp 00000000 fe:00 5662972    /home/jscholz/a.out
0804c000-0804d000 rw-p 00003000 fe:00 5662972    /home/jscholz/a.out
08aeb000-08b0c000 rw-p 08aeb000 00:00 0          [heap]
b7300000-b7321000 rw-p b7300000 00:00 0
b7321000-b7400000
b74a7000-b74a8000 rw-p b74a7000 00:00 0
b74a8000-b75fd000 r-xp 00000000 fe:00 4449500    /lib/i686/cmov/libc-2.7.so
b75fd000-b75fe000 r--p 00155000 fe:00 4449500    /lib/i686/cmov/libc-2.7.so
b75fe000-b7600000 rw-p 00156000 fe:00 4449500    /lib/i686/cmov/libc-2.7.so
b7600000-b7603000 rw-p b7600000 00:00 0
b7603000-b760f000 r-xp 00000000 fe:00 4440067    /lib/libgcc_s.so.1
b760f000-b7610000 rw-p 0000b000 fe:00 4440067    /lib/libgcc_s.so.1
b7610000-b7611000 rw-p b7610000 00:00 0
b7611000-b7635000 r-xp 00000000 fe:00 4449493    /lib/i686/cmov/libm-2.7.so
b7635000-b7637000 rw-p 00023000 fe:00 4449493    /lib/i686/cmov/libm-2.7.so
b7637000-b771a000 r-xp 00000000 fe:00 5865983    /usr/lib/libstdc++.so.6.0.10
b771a000-b771d000 r--p 000e2000 fe:00 5865983    /usr/lib/libstdc++.so.6.0.10
b771d000-b771f000 rw-p 000e5000 fe:00 5865983    /usr/lib/libstdc++.so.6.0.10
b771f000-b7725000 rw-p b771f000 00:00 0
b7730000-b7734000 rw-p b7730000 00:00 0
b7734000-b7735000 r-xp b7734000 00:00 0          [vdso]
b7735000-b774f000 r-xp 00000000 fe:00 4441746    /lib/ld-2.7.so
b774f000-b7751000 rw-p 0001a000 fe:00 4441746    /lib/ld-2.7.so
bfdfd000-bfe12000 rw-p bffea000 00:00 0          [stack]
Aborted
jscholz@fccsci:~$ PuTTYPuTTY


Car.h
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

#include <string>
#include <iomanip>
#include <fstream>
#include <iostream>

using namespace std;



class Car
{
    private:
        string make;
        string model;
        int year;
        double value;
    public:
        Car (string, string, int, double);
        Car ();
        void setMake (string);
        void setModel (string);
        void setYear (int);
        void setValue (double);
        string getMake();
        string getModel();
        int  getYear();
        double getValue();
        void print(ofstream&);
        void print();
        ofstream outFile;
};

Car::Car(string make, string model, int year, double value)
{}

Car::Car() : make("no make"), model("no model"), year(2012), value(0.00)
{}

void Car::setMake(string carMake )
{
    make = carMake;
}

void Car::setModel(string carModel )
{
    model = carModel;
}

void Car::setYear(int carYear)
{
    year = carYear;
    if (year < 1769 || year > 2012)
    {
        year = 2012;
    }
}

void Car::setValue(double carValue)
{
    value = carValue;
    if (value < 0)
    {
        value = 0.00;
    }
}

string Car:: getMake()
{
    return make;
}

string Car::getModel()
{
    return model;
}

int Car::getYear()
{
    return year;
}

double Car::getValue()
{
    return value;
}

void Car::print(ofstream&outFile)
{
    outFile << setw(6) << left << year << setw(15) << left << make << setw(20) << left
         << model << setw(10) << setprecision(2) << fixed << right << value << endl;

}

void Car::print()
{
    cout << setw(6) << left << year << setw(15) << left << make << setw(20) << left
         << model << setw(10) << setprecision(2) << fixed << right << value << endl;
}



Split up because too long - Problem occurs on line 131 of prog4.cpp:

prog4.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "Car.h"

using namespace std;

void readFile(ifstream&, Car[]);
void sortByYear(Car[]);
void sortByValue(Car[]);

int MAX_SIZE=20;

int main ()
{
    string singleMake, singleModel;
    int singleYear;
    double singleValue;
    Car car[MAX_SIZE];
    Car singleCar;
    string ifName, ofName;
    ifstream fin;
    ofstream fout;
    string arrayMake, arrayModel;
    int arrayYear;
    double arrayValue;
    int count=0;

    cout << "Please enter the vehicle's make: ";
    getline (cin, singleMake);
    cout << "Please enter the vehicle's model: ";
    getline (cin, singleModel);
    cout << "Please enter the vehicle's year: ";
    cin >> singleYear;
    cout << "Please enter the vehicle's value: ";
    cin >> singleValue;

    singleCar.setMake(singleMake);
    singleCar.setModel(singleModel);
    singleCar.setYear(singleYear);
    singleCar.setValue(singleValue);

    cout << endl << singleCar.getYear() << " " << singleCar.getMake() << " "
         << singleCar.getModel() << " " << singleCar.getValue();

    for (int m=0; m < MAX_SIZE; m++)
    {
        car[m].print();
    }

    cout << endl << "Please enter the name of the file you wish to open: ";
    cin >> ifName;
    cout << endl << "Please enter the name of the file you wish to write to: ";
    cin >> ofName;
    fin.open(ifName.c_str());
    if (fin.fail())
    {
        cout << "Could not open the specified file.";
    }

    readFile (fin, car);

    for (int x=0; x < MAX_SIZE; x++)
    {
        car[x].print();
    }

    cout << endl << endl;

    sortByYear(car);

    for (int z=0; z < MAX_SIZE; z++)
    {
        car[z].print();
    }

    cout << endl << endl;
    fout << endl << endl;

    sortByValue(car);

    for (int y=0; y < MAX_SIZE; y++)
    {
        car[y].print();
    }

    fout.close();
}


void readFile(ifstream&inFile, Car c[])
{
    for (int count=0; count < MAX_SIZE; count++)
    {
            string aMake, aModel;
            int aYear;
            double aValue;
            getline(inFile, aMake);
            getline(inFile, aModel);
            inFile >> aYear;
            inFile >> aValue;
            c[count].setMake(aMake);
            c[count].setModel(aModel);
            c[count].setYear(aYear);
            c[count].setValue(aValue);
            inFile.ignore(100, '\n');
            inFile.ignore(100, '\n');

            if(inFile.eof())
            {
                break;
            }
    }
}


void sortByYear (Car cArray[])
{
    string tempMake1, tempMake2, tempModel1, tempModel2;
    int tempYear1, tempYear2;
    double tempValue1, tempValue2;
    int minPos = 0;

    for (int i=0; i < MAX_SIZE; i++)
    {

        for (int k=minPos+1; k <= MAX_SIZE; k++)
        {

            if (cArray[minPos].getYear() > cArray[k].getYear())
            {
                tempMake1 = cArray[minPos].getMake();
                tempModel1 = cArray[minPos].getModel();
                tempYear1 = cArray[minPos].getYear();
                tempValue1 = cArray[minPos].getValue();

                tempMake2 = cArray[k].getMake();
                tempModel2 = cArray[k].getModel();
                tempYear2 = cArray[k].getYear();
                tempValue2 = cArray[k].getValue();

                cArray[k].setMake(tempMake1);
                cArray[k].setModel(tempModel1);
                cArray[k].setYear(tempYear1);
                cArray[k].setValue(tempValue1);

                cArray[minPos].setMake(tempMake2);
                cArray[minPos].setModel(tempModel2);
                cArray[minPos].setYear(tempYear2);
                cArray[minPos].setValue(tempValue2);
            }

        }

        minPos++;
    }
}

void sortByValue (Car cArray[])
{
    string tempMake1, tempMake2, tempModel1, tempModel2;
    int tempYear1, tempYear2;
    double tempValue1, tempValue2;
    int minPos = 0;

    for (int i=1; i < MAX_SIZE; i++)
    {
        for (int k=minPos + 1; k < MAX_SIZE; k++)
        {

            if (cArray[k].getValue() > cArray[minPos].getValue())
            {
                tempMake1 = cArray[minPos].getMake();
                tempModel1 = cArray[minPos].getModel();
                tempYear1 = cArray[minPos].getYear();
                tempValue1 = cArray[minPos].getValue();

                tempMake2 = cArray[k].getMake();
                tempModel2 = cArray[k].getModel();
                tempYear2 = cArray[k].getYear();
                tempValue2 = cArray[k].getValue();

                cArray[k].setMake(tempMake1);
                cArray[k].setModel(tempModel1);
                cArray[k].setYear(tempYear1);
                cArray[k].setValue(tempValue1);

                cArray[minPos].setMake(tempMake2);
                cArray[minPos].setModel(tempModel2);
                cArray[minPos].setYear(tempYear2);
                cArray[minPos].setValue(tempValue2);
            }

        }
        minPos++;
    }
}
I haven't actually run the code to prove it - but there maybe a clue here
in these loop checks


here is the SortbyYear loop layout.

1
2
3
4
5
6
7
8
9
10
void sortByYear (Car cArray[])
{
    //Some code here
    int minPos = 0;

    for (int i=0; i < MAX_SIZE; i++)
    {

        for (int k=minPos+1; k <= MAX_SIZE; k++)  // Note k <= MAX_SIZE check
        {



here is the SortbyValue loop:
1
2
3
4
5
6
7
8
9
void sortByValue (Car cArray[])
{
    //some code here 
    int minPos = 0;

    for (int i=1; i < MAX_SIZE; i++)
    {
        for (int k=minPos + 1; k < MAX_SIZE; k++)  //Note  k < MAX_SIZE
        {


Thanks! Got it fixed. I think I had just been staring at it too long. I still don't know why that integer would have fubared it, but it sure did.

Thanks again!
Jade
Topic archived. No new replies allowed.