Dealing with Vector Arrays

/* I am new with vector arrays, This is basically a parking slot which can store 10 vehicles at a time. There are 2 classes, one named parking which stores the value, slot_number, availability which means slot is available or not, and vehicle_numbers. The second class vehicle has a vector array of type parking, now when a object of class is created all the slots are set to Available and now when we want to park a vehicle the values should change, I don't know what should be the function return type and argument, what should the function park_a_vehicle return and same what should be the arguments passed in main , I have commented 3 places as Code missing, please help as to what code should be inserted
*/

#include<iostream>
#include<vector>
#include<string>
using namespace std;
class parking
{
int slot_number;
string availability;
string vehicle_number;

public:

parking(int slotnum,string avail,string vehnum):slot_number(slotnum),availability(avail),vehicle_number(vehnum)
{}
int getslot_number()
{
return slot_number;
}

string getavailability()
{
return availability;
}

string getvehiclenumber()
{
return vehicle_number;
}
void set_slot_number(int new_slot_number)
{
slot_number=new_slot_number;
}
void set_availability(string new_availability)
{
availability=new_availability;
}
void set_vehicle_number(string new_vehicle_number)
{
vehicle_number=new_vehicle_number;
}

};

class vehicle
{
std::vector<parking> veh[10];
std::vector<parking>::iterator p;
public:

vehicle()
{

int i;
for(i=0;i<10;i++)
{
veh[i].push_back(parking(i+1,"Available"," "));
}
}

void display()
{
system("cls");
int i;
cout<<"\n Current Vehicle Status\n";
for(i=0;i<10;i++)
{
for(p=veh[i].begin();p!=veh[i].end();p++)
{
cout<<p->getslot_number()<<" "<<p->getavailability()<<" "<<p->getvehiclenumber();
}
cout<<endl;
}


}

park_a_vehicle() //1. Return type and parameter missing
{
// Code to be inserted
static int count=0;
string your_vehicle_number;
cout<<"\n Please enter your vehicle number";
cin>>your_vehicle_number;
p->set_vehicle_number(your_vehicle_number);
p->set_slot_number(count);
p->set_availability("Not Available");
count++;

// 2. Code Missing as to what should be returned
}

};
int main()
{
vehicle park;
char ch='y';
int choice;
cout<<"\n Please enter a choice";

do
{
cout<<"\n Press 1. to Display the Current Status";
cout<<"\n Press 2 to park a vehicle";
cout<<"\n Press 3 to exit";
cout<<"\n Your Choice: ";
cin>>choice;

switch(choice)
{
case 1:
park.display();
break;

case 2:
park.park_a_vehicle(); // 3. Arguments Missing
break;

case 3:
exit(0);
break;

default:
cout<<"\n Wrong choice";
break;
}

cout<<"\n Any more time you wanna enter (Y/N) ";
cin>>ch;


}while(ch=='Y' || ch=='y');


getchar();
return 0;
}
Last edited on
1. park_a_vehicle can be declared as type void since it doesn't need to return anything.

2. If declared void, nothing needs to be returned.

I think you need to reconsider your class structure. To me, your parking class represents a parking space within a parking lot which may or may not be occupied by a vehicle. It makes no sense to me to have each vehicle instance contain a vector of parking information. A "parking lot" is a vector of some number of parking spaces. vehicle_number should be an attribute of vehicle.

Some other errors:

Line 79: park_a_vehicle is called as a member function of park, but your park class has no such member function.

Line 113: display() is called as a member function of park, but your park class has no such member function.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/




The logic in your code is hard to understand (and the lack of code tags does not help).
What does a 'parking' represent?
What does a 'vechile' represent?


Then the std::vector<parking> veh[10];
A 'veh' is an array of 10 objects. Each object is a separate std::vector of parking objects. You are of course free to name classes as you wish, but I don't usually think that my (only) car has 42 parking slots, of which 7 are free.
Topic archived. No new replies allowed.