Default constructor not constructing....defaultly

Hey guys! I was just wondering if anyone could take a look at this and let me know why my array (of type Car) isn't constructing with "no make", "no model", "2012" and "0.00". I thought I had it right in the default constructor, but when I try to just output the array without adding any file data into it, i get a bunch of zeroes and garbage, instead of 20 "no make, no model, 2012, 0.00" entries like I was expecting. Any help you can provide is much appreciated!!

Thanks,

Jade

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
#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);
        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="no make", string model="no model", int year=2012, double 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;
}



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
200
201
202
203
204
205
206
207
208
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include "Car.h"

using namespace std;

void readFile(ifstream&, Car[], int, string, string, int, double);
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.";
    }

    for (int i=0; i < MAX_SIZE; i++)
    {
        readFile (fin, car, i, arrayMake, arrayModel, arrayYear, arrayValue);
    }

    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[], int count, string aMake, string aModel, int aYear, double aValue)
{
    while(!inFile.eof() && count < MAX_SIZE)
    {
        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');
    }
}

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

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

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

            if (cArray[i].getYear() > cArray[minPos].getYear())
            {
                madeSwap = 1;
            }

            if (madeSwap == 1)
            {
                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;
    int madeSwap;

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

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

            if (cArray[i].getValue() > cArray[minPos].getValue())
            {
                madeSwap = 1;
            }

            if (madeSwap == 1)
            {
                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++;
        }
    }
}

In the constructor you are not initializing the member variables. All you have done is specifying the default arguments of the constructor.
Thank you! Got it fixed!

Jade
Topic archived. No new replies allowed.