array of structs project

Ok ive got this mostly done but, i need a counter for my list that DOES not start with the array element number of 0. the only problem is if i do a nested for/while itll mess up my if and else structure and i need to increment after each car even if it isnt printed. help?

Project:

program in C++ that stores cars in an array and that can add, remove, print and list cars. The commands for adding, removing, printing and listing will come from a file (specified on either the command-line or via STDIN). Commands are as follows (fields are separated by tabs):
A <price> <year> <mileage> <make> <model> <color> <distance> Add a new car.
R <price> <year> <mileage> <make> <model> <color> <distance> Remove the specified car.
P <price> <year> <mileage> <make> <model> <color> <distance> Print the specified car.
L List all of the cars currently in the database.




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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>


using namespace std;






struct Vehicles{
	
	char actionNeeded;
	string make;
	string model;
	string color;
	int price;
	int mileage;
	int year;
	int distance;
	
};


void printCars(Vehicles);
//void getCars (Vehicles& , ifstream& );
void addCars (Vehicles& , ifstream& );
void listCars (Vehicles *car);
void removeCars(Vehicles*, ifstream&,int);
int main()

{
	//ifstream inData;
	//inData.open("Vinfo.txt");
	
	/*cout << "Enter the input data file name:" << endl;
	
	cin >> infileName;
	
	inData.open(infileName.c_str());
	
	// make it a loop error message so user doesnt have to restart program. Annoying 
	if(!inData)
	{
		cerr << "Can't open: "  << infileName  << ". Program is stopping" << endl;
		return 1;
		
	}
	*/
	
	
	Vehicles carInfo[1024]={0};
	Vehicles onecar;
	ifstream inData;
	char actionNeeded;
	int numcars=0;
	int h=1;
	
	

	
	for( int i=0; i<1024; i++)
	{
		carInfo[i].price=0;
		
		
	}
	
	
	inData.open("Vinfo.txt");
	




while (inData)
{

	
	inData>>actionNeeded;
	
	if( !inData)
	{
		break;
	}
		

	
	



	

	
switch(actionNeeded)
{
  case 'A':
    //Vehicles car;
    //getcars(car, inData);
    //Add car into array.
  {
	addCars(carInfo[numcars], inData);
	numcars++;
	 // cout <<carInfo[0].price;
	  
  }

  

    break;
  case 'R':
   // Vehicles car;
    //getcars(car, inData);
    //Remove car
  //if(carInfo[i].price==0)
 removeCars(carInfo,inData ,numcars);
  
  
  
    break;
  case 'L': 
	//  cout << "Listing all cars"<< endl;
	  //for counter
	
  
	listCars(carInfo);
  
	
	
    //list cars
  break;
	case 'P':
		/*for( int i=0; i<numcars; i++)
	{
		printCars(carInfo[i]);
		
	}*/
	
		
	break;

}}

	
	//cout << carInfo[0].price<< endl;





	
	
	
	
	return 0;
	
	
}


	


void printCars(Vehicles onecar)
{
			if(onecar.price==0)
	{
		cout << "cant find car" << endl;
	}
	
	else
	{
	cout << onecar.price;
    cout << onecar.year;
	cout << onecar.mileage;
	cout << onecar.make;
	cout << onecar.model;
	cout << onecar.color;
	cout << onecar.distance;
	}
	
	
}


void addCars (Vehicles&  onecar, ifstream& in )
{
	 
	
	
	in >> onecar.price;
	in >> onecar.year;
	in >> onecar.mileage;
	in >> onecar.make;
	in >> onecar.model;
	in>> onecar.color;
	in >> onecar.distance;
	
}

void listCars (Vehicles *car)
	{

	
			
	for (int i=0; i<1024; i++)
		{
		
			
				if(car[i].price==0)
				{
					// car has been removed
					
				}
				
				
		
				else 
			
				{
			
					
	
					cout << "Vehicle " << "#" << i <<":"<< endl;
					
			
					cout<< "---------------------" << endl;
					cout << "Price: " << car[i].price << endl;
					cout <<"Year: " << car[i].year << endl;
					cout << "Miles: " << car[i].mileage  << endl;
					cout << "Make: " << car[i].make  << endl;
					cout << "Model: " <<	car[i].model << endl;
					cout << "Color: " << car[i].color  << endl;
					cout << "Distance: " << car[i].distance  << endl;
					cout << endl;
			
				}
			
		
	
		
		}	
	


	}	
void removeCars(Vehicles *car, ifstream& in , int numcars)
{
	Vehicles onecar;
	in >> onecar.price;
	in >> onecar.year;
	in >> onecar.mileage;
	in >> onecar.make;
	in >> onecar.model;
	in >> onecar.color;
	in >> onecar.distance;
	
	
	for(int i=0; i<numcars; i++)
	{  
		if(car[i].price==onecar.price)
		{
			if(car[i].year==onecar.year)
			
				if (car[i].mileage==onecar.mileage)
				
					if(car[i].make==onecar.make)
					
						if(car[i].model==onecar.model)
						
							if(car[i].color==onecar.color)
							
								if(car[i].distance==onecar.distance)
								{
									car[i].price=0;
									
									};
									
								
						
							}
			//do ifs for rest of it
	}
	//change onecar.price to something like -1

	
}


input file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
A	7989	2008	93843	Toyota	Prius	Gray	405 
A	8993	2005	85227	Honda	Civic	White	377
A	8998	2006	82015	Honda	Insight	Red	441
R	8993	2005	85227	Honda	Civic	White	377
L
A	9800	2007	65434	Toyota	Prius	Gray	465
A	9995	2010	42000	Honda	Insight	Silver	464
R	8998	2006	82015	Honda	Insight	Red	441
R	9995	2010	42000	Honda	Insight	Silver	464
A	9998	2008	92653	Toyota	Prius	Red	200
A	10000	2008	85096	Toyota	Prius	Unspecified	405
A	10000	2010	100000	Honda	Insight	Gray	470
A	10000	2008	87183	Toyota	Prius	Pink	576
R	10000	2010	100000	Honda	Insight	Gray	470
L
R	9999	2005	74446	Honda	Civic	Blue	595
P
Last edited on
bump
bump
Topic archived. No new replies allowed.