Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to change the array code to vector but it doesnt work? Can anyone help me out?

What I have tried:

Array
void displayConfirmBooking(Customer customer[], int size) {
	cout << "Confirmed booking:\n";

	for (int i = 0; i < size; i++) {
		if (customer[i].isBooked()) {
			cout << customer[i].getName() << " is booked\n";
		}
	}
}

Vector

<pre>void displayConfirmBooking(vector<Customer>customers){
	cout << "Confirmed booking:\n";

	for (int i=0; i < customers.size(); i++){
		if (customers[i].isBooked()){
			cout << customers[i].getName() << " is booked\n";
		}
	}
}
Posted
Updated 28-Oct-21 6:53am
Comments
Richard MacCutchan 28-Oct-21 12:43pm    
What is the definition of the vector, and the class it contains?
Choi Yoojung 28-Oct-21 12:55pm    
Here is the full code:

void confirmBooking(Customer customer[], int size);

void displayConfirmBooking(vector<customer>customers){
cout << "Confirmed booking:\n";

for (int i=0; i < customers.size(); i++){
if (customers[i].isBooked()){
cout << customers[i].getName() << " is booked\n";
}
}
}

for (int i = 0; i < size; i++) {
if (customer[i].isBooked()) {

int main(){
const int SIZE = 2;
Customer customerList[SIZE];

Customer myObj;

vector<customer> customers;

for (int i=0; i<size; i++){
="" string="" name;
="" int="" age;
="" bool="" student="false;
" char="" answer;

="" cout="" <<="" "please="" enter="" name:="" ";
="" getline(cin,name);
="" age:="" cin="">> age;
cout << "Is student ID present? (y/n)";
cin >> answer;
answer = ::tolower(answer);
if (answer == 'y')
student = true;

Customer customer(name,age,student);
customerList[i] = customer;
cin.ignore(100,'\n');
cout << endl;
}

confirmBooking(customerList, SIZE);
customers.push_back(myObj);
displayConfirmBooking(customers);
return 0;
}

void confirmBooking(Customer customer[], int size){
const double TICKET_PRICE = 56.0;
const double MEAL_PRICE = 30.0;

for (int j=0; j<size; j++){
="" double="" tempticketprice,="" tempmealprice;

="" if="" (customer[j].ischild()){
="" tempticketprice="TICKET_PRICE" 2;
="" tempmealprice="MEAL_PRICE" }
="" else="" (customer[j].isstudent()){
="" *="" 0.9;
="" else{
="" 0.8;
="" }

="" cout="" <<="" customer[j].getname()="" "\n"="" <<
="" "ticket="" price:="" rm"="" "meal="" "total="" +="" endl;

="" char="" answer;
="" "confirm="" booking="" for="" "="" (y="" n)";
="" cin="">> answer;
answer = ::tolower(answer);

if (answer == 'y'){
customer[j].setBooked(true);
cout << "Booked\n\n";
}
else{
customer[j].setBooked(false);
cout << "Not booked\n\n";
}

}
}

1 solution

In displayConfirmBooking, you are passing the vector by value--that is, creating a copy of it. You need to pass it by reference so that the one supplied by the caller will actually be updated: vector<Customer>& customers.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900