Classes

Is there a way to get an array within main() to operate in a class (with private and public objects?) My input data with causing problem with the compiler. Could you guys and gals point me in the right direction. I have thoroughly read the tutorials, but seem to still not be getting the idea.

Thanks!
You can pass the array into one of the member functions of the class.

 
(class variable).memberfunction(array variable);


Of course you will have to set the class member to accept the array input as well;
Last edited on
If codegoggles response didn't help, then post your code (using code tags) and indicate specifically where you're having problems.
In the main() function starting in line103, I have an array of numCar. I setup temp(variables) in the main so that the code would work within the main function. However, they are not communicating outside of the main function. How do I get them to see each other?

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
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std; 

class CarData //defining class "wrapper"
    {
    private:
        double year, mileage;
        string make, model, name, phone;
    public:
        void setYear(double);//mutator functions
        void setMileage(double);
        void setMake(string);        
        void setModel(string); 
        void setName(string);   
        void setPhone(string);      

        double getYear();// accessor functions
        double getMileage();
        string getMake();
        string getModel();
        string getName();
        string getPhone(); 

        void valid_mileage(double);
        void car_details();
    };         

//setYear will assign a value to the private member year
void CarData::setYear(double tempyear)
{
    year = tempyear;
}
//setMileage will assign a value to the private member mileage
void CarData::setMileage(double tempmileage)
{
    mileage = tempmileage;
}
//setMake will assign a value to the private member make
void CarData::setMake(string tempmake)
{
    make = tempmake;
}
//setModel will assign a value to the private member model
void CarData::setModel(string tempmodel)
{
    model = tempmodel;
}
//setName will assign a value to the private member name
void CarData::setName(string tempname)
{
    name = tempname;
}
//setPhone will assign a value to the private member phone
void CarData::setPhone(string tempphone)
{
    phone = tempphone;
}

double CarData::getYear()
{
    return year;
}

double CarData::getMileage()
{
    return mileage;
}

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

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

string CarData::getName()
{
    return name;
}

string CarData::getPhone()
{
    return phone;
}

void CarData::valid_mileage(double mileage)
{
    if (mileage>=0)
        CarData::mileage=mileage;
    else  {
        CarData::mileage=0;
        cout << "Invalid mileage!\n";
    }
}

//main function
int main()
{
    CarData customerCar;
    int numCar=0, ctr;
    double tempyear[numCar], tempmileage[numCar];// local variables
    string tempmake[numCar], tempmodel[numCar], tempname[numCar], tempphone[numCar];
    cout << "How many new vehicles are being added to the inventory?\n";
    cin >> numCar;
    if (numCar < 1)
        {
        system ("PAUSE");
        return 0;
        }
    CarData car[numCar]; //creates an car array of "numCar" amounts of the "CarData4 type
    
    for (ctr = 0; ctr < numCar; ctr++)// for populating the array
    {
        cout << "\n\nVehicle " << ctr +1; // Helps user keep track vehicle #
        cout << "\n\tEnter year #: "; // Input information
        cin.ignore();        
        cin >> tempyear[ctr];

        while (tempyear[ctr] < 1910 || tempyear[ctr] > 2014)
            {
            cout << "Invalid year.  Please enter the correct year. ";
            cin.clear();
            cin.ignore();
            cin >> tempyear[ctr];
            }
       
        cout << endl;

        cout << "\n\tEnter make: "; // Input information
        cin.ignore();
        getline (cin, tempmake[ctr]);
        cout << endl;
        
        cout << "\n\tEnter model: "; // Input information
        cin.ignore();
        getline (cin, tempmodel[ctr]);
        cout << "test";
        cout << endl;   

        cout << "\n\tEnter mileage #: "; // Input information  
        cin.ignore();   
        cin >> tempmileage[ctr];
        while (tempmileage[ctr] < 0 || tempyear[ctr] > 1000000)
            {
            cout << "Invalid mileage.  Please enter the correct mileage. ";
            cin.clear();
            cin.ignore();
            cin >> tempmileage[ctr];
            }
        cout << endl;     

        cout << "\n\tEnter owner's first and last name: (ex. John Smith)"; // Input information
        cin.ignore();        
        getline (cin, tempname[ctr]);
        cout << endl;

        cout << "\n\tEnter owner's phone #: (ex. (555)555-5555)"; // Input information      
        getline (cin, tempphone[ctr]);
        cout << endl;
         
    }    

    customerCar.setYear(tempyear[ctr]);
    customerCar.setMileage(tempmileage[ctr]);
    customerCar.setMake(tempmake[ctr]);
    customerCar.setModel(tempmodel[ctr]);
    customerCar.setName(tempname[ctr]);
    customerCar.setPhone(tempphone[ctr]);

    cout << "\n\tName\t|Year\t|Make\t|Model\t|Mileage\t|Phone Number" << endl;
        for (ctr = 0; ctr < numCar; ctr++){
            cout << "\t" << tempname[ctr];
            cout << "\t" << tempyear[ctr];
            cout << "\t" << tempmake[ctr];
            cout << "\t " << tempmodel[ctr];
            cout << "\t " << tempmileage[ctr];
            cout << "\t\t " << tempphone[ctr] << endl;
            cout << endl << endl;
        }
//Trying to output a .txt file
/*    ofstream my_file;//create instance of the object my_file

    my_file.open ("c:example_1.txt");//creates empty .txt file
    my_file << for (ctr = 0; ctr < numCar; ctr++)
                cout << "\n" << car[numCar];
*/
    system("PAUSE");
    return 0;
}
Lines 106-108: How many occurrances of these variables do you think are allocated? Hint: numCar=0.

Line 116: You can't dynamically allocate an array like this in C++. (Some compilers allow this, but it's not in the standard).

Line 123 (and numerous other places): This isn't going to work because there is no space allocated for the variable.

Lines 169-174: You're indexing past the end of the various arrays (assuming they were allocated correctly). When you exit the for loop at line 167, ctr is going to be equal to NumCar, which is past the end of the arrays.

they are not communicating outside of the main function.

I'm not understanding the question. Can you give an example of what you mean?

BTW, you really don't need the temp arrays. Only one instance of each variable is really needed. You should be calling car's various setters within the for loop.
I deleted my last post as Abstraction covers my question. But just to clarify:

You can't dynamically allocate an array like this in C++. (Some compilers allow this, but it's not in the standard).


If the array is undetermined at compile time as it is (because it request user input for the size, it will determined at runtime), you need to allocate the array dynamically using new keyword. If you dynamically allocate memory you need to return the memory once done with it with the delete keyword or in the case of a dynamic array the delete[] keyword.

I still don't know why you are assigning the values to the customercar, why bother with the loop at all if you are only assigning to customercar and not the array of cars (that you never created ;P).
Last edited on
Here I've modified the code and added comments study this to see how its been changed. NOTE: I don't know if this is what you are intending but it works,
and this was my interpretation of the code and how you wanted it to work.

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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string> // included the string header for use of the c++ string type

using namespace std;

class CarData //defining class "wrapper"
{
private:
	double mileage;
	int year; // type int
	string make, model, name, phone;
public:
	void setYear(int);//mutator functions
	// changed to an int as a double value was excessive memory allocation
	// and years are not decimal. I changed your member functions
	// to reflect this
	void setMileage(double);
	void setMake(string);
	void setModel(string);
	void setName(string);
	void setPhone(string);

	int getYear();// accessor functions
	double getMileage();
	string getMake();
	string getModel();
	string getName();
	string getPhone();

	// New members see below comment for details
	string stringInput();
	int intInput();
	double doubleInput();

	void valid_mileage(double);
	void car_details();
};

/* 
So for your input I added members to take care
of the input for a couple of reasons. Firstly it eliminates redundant code,
secondly passing cin inputs into members requires storage varibles, as i cant access
and shouldn't access the private member varibles directly, this will suffice.
*/

//setYear will assign a value to the private member year
void CarData::setYear(int tempyear)
{
	year = tempyear;
}
//setMileage will assign a value to the private member mileage
void CarData::setMileage(double tempmileage)
{
	mileage = tempmileage;
}
//setMake will assign a value to the private member make
void CarData::setMake(string tempmake)
{
	make = tempmake;
}
//setModel will assign a value to the private member model
void CarData::setModel(string tempmodel)
{
	model = tempmodel;
}
//setName will assign a value to the private member name
void CarData::setName(string tempname)
{
	name = tempname;
}
//setPhone will assign a value to the private member phone
void CarData::setPhone(string tempphone)
{
	phone = tempphone;
}

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

double CarData::getMileage()
{
	return mileage;
}

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

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

string CarData::getName()
{
	return name;
}

string CarData::getPhone()
{
	return phone;
}

string stringInput()
{
	string tempstring;
	getline(cin, tempstring, '\n');
	cout << endl;
	return tempstring;
}

int intInput()
{
	int tempint;
	cin >> tempint;
	cout << endl;
	return tempint;
}

double doubleInput()
{
	double tempdouble;
	cin >> tempdouble;
	cout << endl;
	return tempdouble;
}

void CarData::valid_mileage(double mileage)
{
	if (mileage >= 0)
		CarData::mileage = mileage;
	else
	{
		CarData::mileage <= 0;
		cout << "Invalid mileage!\n";
	}
}

//main function
int main()
{
	//CarData customerCar; <-- commented out as I don't understand see my post.
	int numCar = 0; // <-- removed the ctr declaration here because it is included
		// in the for loop and it's good programming practice to defer declaration 
		// until the varible needed. 
		//( it is also good programming practice to initialize on creation )

	// <-- Removed the temp arrays
	cout << "How many new vehicles are being added to the inventory?\n";
	cin >> numCar;
	if (numCar < 1)
	{
		system("PAUSE");
		return 0;
	}
	CarData* car = new CarData[numCar]; //creates a dynamic array of "numCar" amounts of the "CarData4 type
	// with a pointer locating it named car

	for (int ctr = 0; ctr < numCar; ctr++)// for populating the array
	{	
		cin.clear();
		cout << "\n\nVehicle " << ctr + 1; // Helps user keep track vehicle #
		cout << "\n\tEnter year #: "; // Input information
		car[ctr].setYear(intInput()); // changed to the set member function

		while (car[ctr].getYear() < 1910 || car[ctr].getYear() > 2014) // <-- changed to the get member function
		{
			cout << "Invalid year. \nPlease enter the correct year: ";
			car[ctr].setYear(intInput()); // Changed
		}
		
		cin.ignore();
		cout << "\n\tEnter make: "; // Input information
		car[ctr].setMake(stringInput());

		cout << "\n\tEnter model: "; // Input information
		car[ctr].setModel(stringInput());

		cout << "\n\tEnter mileage #: "; // Input information  
		car[ctr].setMileage(doubleInput());

		while (car[ctr].getMileage() < 0 || car[ctr].getMileage() > 1000000) // changed
		{
			cout << "Invalid mileage.  \nPlease enter the correct mileage: ";
			car[ctr].setMileage(doubleInput());
		}

		cin.ignore();
		cout << "\n\tEnter owner's first and last name: (ex. John Smith)"; // Input information
		car[ctr].setName(stringInput());

		cout << "\n\tEnter owner's phone #: (ex. (555)555-5555)"; // Input information      
		car[ctr].setPhone(stringInput());

	}

	/*customerCar.setYear(tempyear[ctr]);
	customerCar.setMileage(tempmileage[ctr]);
	customerCar.setMake(tempmake[ctr]);
	customerCar.setModel(tempmodel[ctr]);
	customerCar.setName(tempname[ctr]);
	customerCar.setPhone(tempphone[ctr]);*/ // <-- commented out as I dont understand see post

	cout << "\n\tName\t|Year\t|Make\t|Model\t|Mileage\t|Phone Number" << endl;
	for (int ctr = 0; ctr < numCar; ctr++)
	{
		cout << "\t" << car[ctr].getName();
		cout << "\t" << car[ctr].getYear();
		cout << "\t" << car[ctr].getMake();
		cout << "\t " << car[ctr].getModel();
		cout << "\t " << car[ctr].getMileage();
		cout << "\t\t " << car[ctr].getPhone() << endl;
		cout << endl << endl;
	}
	//Trying to output a .txt file
	/*    ofstream my_file;//create instance of the object my_file

	my_file.open ("c:example_1.txt");//creates empty .txt file
	my_file << for (ctr = 0; ctr < numCar; ctr++)
	cout << "\n" << car[numCar];
	*/

	// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< IMPORTANT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	delete[] car; // You need to release dynamic memory see post
        car = nullptr; // make pointer null after release
	
	system("PAUSE");
	return 0;
Last edited on
Topic archived. No new replies allowed.