Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have done the front part and now I want to creat a function in the main.cpp which takes an instance of Customer as a parameter. All this function should do is print out the name of the customer if (and only if) they are booked.

I am lost, because my book status is in main.cpp how can i retrive the book status ?

Example output:

Confirmed booking:
Tony Stark is booked
Peter Parker is booked
Wanda Maximoff is booked
Bruce Banner is book

What I have tried:

#include <iostream>
#include "customer.h"
using namespace std;

void ConfirmBooking(Customer n,Customer a,Customer s,Customer c);
void show(Customer n);

int main() {

	Customer myObj;
		
	string name;
	int age;
	bool student;
	char id;
	int i;
	

	for(i=0;i<2;i++){
 	
		myObj.info();
		myObj.isStudent();
		myObj.isChild();
	
		ConfirmBooking(myObj,myObj,myObj,myObj);
	}
	
		show(myObj);

//		Customer myObj;
//		myObj.show(n,a,s);
}

void ConfirmBooking(Customer n ,Customer a,Customer s,Customer c){
			
			string name = n.name;
			int age = a.age;
			bool isStudent = s.student;
			bool isChild = c.child;
			double ticketPrice,mealPrice;
			char confirm ;
			bool book;
			

			//children
			if(isChild == true && isStudent == true){
				
				ticketPrice = 50.0 / 100.0 * 56; ;
				mealPrice = 50.0 / 100.0 * 30;
				
			//student	
			}else if(isStudent == true){
			
				ticketPrice = 50.0 / 100.0 * 56;
				mealPrice = 90.0 / 100.0 * 30;
				
			}else if(isChild == true){
			
				ticketPrice = 50.0 / 100.0 * 56;
				mealPrice = 50.0 / 100.0 * 30;
				
			}else{
				
				ticketPrice = 80.0 / 100.0 * 56;
				mealPrice = 90.0 / 100.0 * 30;
				
			}
			
			cout << name << " " ;
			cout << "Ticket Price:RM" << ticketPrice << endl;
			cout << "Meal Price:RM" << mealPrice << endl;
			cout << "Total Price:RM" << ticketPrice + mealPrice << endl;
			cout << "Confirm Booking for " << name << "(y/n)";
			cin >> confirm;
			
			if(confirm == 'y'){
				book = true;
				cout << "Booked\n";
								
			}else if(confirm == 'n'){
				book = false;
				cout << "Not Booked\n";
				
			}else{
				book = false;
			} 
			
			cout << "\n";
		}; 



Header file
#include <iostream>
using namespace std;

class Customer {
	
	public:
		string name;
		int age;
		bool student;
		bool child ;
		char id;

	public:

		Show(string n, int a, bool s, bool c){ 
		    name = n;
		    age = a;
		    student = s;
		    child = c;
		}

	  
	void setName(string n) {
		  name = n;
		}

		string getName() {
		  return name;
		}

		void setAge(int a) {
		  age = a;
		}

		int getAge() {
		  return age;
		}

		void setStudent(bool = true) {
		  student = true;
		}

		bool getStudent() {
		  return student;
		}
		
	/*	void setChild(bool = true) {
		  child = true;
		}

		bool getChild() {
		  return child;
		}  */
		
	void info(){
		cout << "Please enter name:";
		getline(cin >> ws, name);
			
		cout << "Please enter age:";
		cin >> age;
		
		cout << "Is student ID present?(y/n)";
		cin >> id;
		cout << endl; 
		} 
		
	void isStudent(){
			
		if (id == 'y') {

		student = true;
		}
		else if (id == 'n') {
		
			student = false;
		}
		else {
			cout << "Please enter y or n!" << endl;
		}
	}
		
	void isChild(){
			
	if(age >= 5 && age <= 16){
			child = true;
		}else{
			child = false;
			}
		}	
	}; 
Posted
Updated 18-Oct-21 20:57pm
Comments
Richard MacCutchan 18-Oct-21 12:21pm    
You have coded a constructor for your class but you called it Show instead of Customer.
KarstenK 18-Oct-21 14:00pm    
the ConfirmBooking belongs in the Customer class. Some poor design which leads to your bug. Think about it ;-)
merano99 18-Oct-21 17:22pm    
We discussed that yesterday. Havent you seen that?

https://www.codeproject.com/Questions/5315365/This-is-a-Cplusplus-ticket-booking-program-can-any

We discussed that yesterday. Havent you seen that?

https://www.codeproject.com/Questions/5315365/This-is-a-Cplusplus-ticket-booking-program-can-any

And it would be polite to say thank you and rate any helpful hints.
 
Share this answer
 
Comments
Choi Yoojung 19-Oct-21 0:24am    
ya but im not able to edit and reply that post not sure why, thats why i create the new one, really thankyou for the help appreciate it !
The main problem is that you are only creating one instance of a Customer:
Customer myObj;

So every time you go around the loop, you overwrite the previous values and end up with just the one set of Customer information.

You need to create different instances - using the new keyword - and store all the instances in a collection of some sort. Since I have no idea where you are in your course, I can't say "use this", so go back to your course notes and re-read them carefully, paying careful attention when new is discussed.
 
Share this answer
 
Comments
Choi Yoojung 19-Oct-21 0:27am    
oh I see, I will try to fix it thankyou for the solution !
You have a function call ConfirmBooking. It is a simple matter to make that function return a boolean variable to indicate the result. You can save that value and do other things with it later. You might want to add a member variable to the Customer class to hold it also.
 
Share this answer
 
Comments
Choi Yoojung 19-Oct-21 0:25am    
i will try it out ! thanks!
I added the vector and it works perfect !

void show(vector<Customer>customers) {

    cout << "Confirmed Booking: " << endl;
    for (int i = 0; i < customers.size(); i++) {
        if (customers[i].book == true) {
            cout << customers[i].name << " is Booked" << endl;
        }
    }
}

int main() {
		
	string name;
	int age;
	bool student;
	char id;
	int i;
	
	Customer myObj;
	
	vector<Customer> customers;

	for(i=0;i<8;i++){
 	
		myObj.info();
		myObj.isStudent();
		myObj.isChild();
	
		ConfirmBooking(myObj);
		myObj.getConfirmation();
        myObj.setConfirm();
		customers.push_back(myObj);
	}
	
  show(customers);	
}
 
Share this answer
 

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