Can NOT get this array of pointers to an array of class objects to sort properly

Okay, I've tried this a few different ways now with varying results. What I'm trying to do is sort an array of Car class pointers (while leaving the actual Car array unsorted). There are two different versions of the function written below. The one that's active right now causes a memory leak apparently, and it won't get past the prompt for the file name. The one that's commented out at the moment lets the program run just fine, but doesn't actually sort anything.

If there's anybody that can look at this and tell me what I might be doing wrong, I would appreciate it so much. This is literally the last thing I have to do before the semester is over. So it figures that it's giving me huge amounts of trouble, haha.

Jade


prog6.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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#include "Car.h"

void readFile (ifstream&, Car[]);
void sortByModel (Car*[]);
void searchByModel (Car[], Car*[], string, int&);
void assignPointers (Car[], Car*[]);

int main()
{
    Car* carPtr[10];
    Car carArray[10];

    ifstream fin;
    string ifName;
    string modelSearch;
    int searchResult;

    cout << "Enter the name of the file you want to open: ";
    cin >> ifName;

    fin.open(ifName.c_str());

    if (fin.fail())
    {
        cout << "Could not open the specified file.";
    }

    readFile (fin, carArray);

    assignPointers(carArray, carPtr);

    sortByModel (carPtr);

    for (int z=0; z < 10; z++)
    {
        carPtr[z]->print();
    }

    cout << endl << endl << "Enter a vehicle model to search: ";
    cin >> modelSearch;
    searchByModel (carArray, carPtr, modelSearch, searchResult);

    if (searchResult >= 0)
    {
        cout << endl << "Your vehicle model was found in array position "
             << searchResult << "." << endl << endl;
    }

    else
    {
        cout << endl << "Your vehicle model was not found in the array."
             << endl << endl;
    }


}

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

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

    }

}

void assignPointers(Car carArr[], Car*carPoint[])
{
    for (int j=0; j < 10; j++)
    {
        carPoint[j] = &(carArr[j]);
    }
}

void sortByModel(Car* carPt[])
{
    bool swap;
    Car* tempCar;
    do
    {
        swap = false;
        for (int count=0; count < 9; count++)
        {
            if (carPt[count]->getModel() > carPt[count+1]->getModel())
            {
                tempCar = carPt[count];
                carPt[count] = carPt[count+1];
                carPt[count+1] = tempCar;
            }
            swap = true;
        }

    }
    while (swap);
}



//void sortByModel (Car carArr[], Car*carPt[])

//{

//  int minPos = 0;
//  int tempVal;
//
//  for (int i=10; i < 10; i++)
//  {
//      for (int k=minPos+1; k < 10; k++)
//      {
//          if (carArr[k].getModel() < carArr[minPos].getModel())
//          {
//              tempVal = k;
//              k = minPos;
//              minPos = tempVal;
//
//          }
//
//      }
//  minPos++;
//  }
//}

void searchByModel (Car carArr[], Car*carPt[], string modelIn, int&itemFound)
{

    string tempModel;
    itemFound = -1;

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

        tempModel = carArr[i].getModel();

        if (tempModel == modelIn)
        {
            itemFound = i;
        }

    }
}


Car.h Header file
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
/*************************
* Jade Scholz            *
* CSCI 123 - Spring 2012 *
* Program # 6            *
* Due 05.25.12           *
*************************/


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

using namespace std;



class Car
{
    private:            // Fields for Car class
        string make;
        string model;
        int year;
        double value;
    public:
        Car (string, string, int, double);
        Car ();
        void setMake (string);  // Sets car make
        void setModel (string); // Sets car model
        void setYear (int);     // Sets car year
        void setValue (double); // Sets car value
        string getMake();       // Returns car make
        string getModel();      // Returns car model
        int  getYear();         // Returns car year
        double getValue();      // Returns car value
        void print(ofstream&);  // Prints data to output file
        void print();           // Prints data to screen
        ofstream outFile;
};

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

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

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

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

void Car::setYear(int carYear)          // Sets car year
{
    year = carYear;             // If year is less than 1769, more than 2012, returns 2012
    if (year < 1769 || year > 2012)
    {
        year = 2012;
    }
}

void Car::setValue(double carValue) // Sets car value (sets to 0 if negative)
{
    value = carValue;
    if (value < 0)
    {
        value = 0.00;
    }
}

string Car:: getMake()      // Returns car make
{
    return make;
}

string Car::getModel()      // Returns car model
{
    return model;
}

int Car::getYear()          // Returns car year
{
    return year;
}

double Car::getValue()      // Returns car value
{
    return value;
}

void Car::print(ofstream&outFile)       // Prints car data out to file
{
    outFile << setw(6) << left << year << setw(15) << left << make << setw(20) << left
         << model << setw(10) << setprecision(2) << fixed << right << value << endl;

}

void Car::print()                       // Prints car data out to screen
{
    cout << setw(6) << left << year << setw(15) << left << make << setw(20) << left
         << model << setw(10) << setprecision(2) << fixed << right << value << endl;
}


cars.txt sample file
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
Ford
Mustang
2000
12312.00

Ford
Mustang
2005
32918.00

Ford
Mustang
2012
65230.00

Ford
F-350 Super Duty
2011
82152.00

BMW
M5
2006
67982.00

Aston Martin
V8 Vantage
2009
98231.00

Lamborghini
Aventador
2011
123151.00
Simple mistake. Read this a little more carefully.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
do
    {
        swap = false;
        for (int count=0; count < 9; count++)
        {
            if (carPt[count]->getModel() > carPt[count+1]->getModel())
            {
                tempCar = carPt[count];
                carPt[count] = carPt[count+1];
                carPt[count+1] = tempCar;
            }
            swap = true;
        }

    }
    while (swap);

When is swap supposed to be true? When is it not supposed to be true?
In your code, when is swap false?

Whenever a program simply hangs like that, always look at your loop control variables.
Last edited on
Topic archived. No new replies allowed.