Hey, am working on Bus reservation system and having some problem...
I am trying to fix something thats wrong with the code..but doesnt seems to get it..Please help..
You have been asked to program a system that performs bus ticket reservation. Assume that there are 5 busses. Each bus starts at Melaka and travels within Malaysia only once in a day. You have to decide their schedules and destinations.
Design your program with appropriate input. When a passenger wants to book a ticket,
i. request for the destination and inform the bus schedule;
ii. display the seat arrangement in the bus and indicate the available seats;
iii. let the passenger select the seat (any two passengers cannot book the same seat); and
iv. upon confirmation, print the payment and travel details, as well as indicate the seat number of the passenger.
Include iterations to accept more than one passenger.
I think we need to review how to do classes with you friend.
Let's start with Line 17, please refrain from declaring the number of buses you have this way. You should have leave line 17 like this: }bus;
and allow the user declare the number of buses they have in the Main function. so you would have: bus buses[4]; somewhere early on in the int main(...) function. This sets up 5 objects of the atrans class for you. If taken in the imaginary context though we would want the owner of the terminal to declare the number of buses in a "settings file".
Down to line 24:
- This member function is populating an object so I think it is better off using the constuctor.
- If you made the change I mentioned above then you don't need the '[p]' anywhere in here.
First...asked for 5 buses..so i need you to help me if user enter more than 5 buses should give error...and also...can u try the std::string ,std::vector...Please read the question and understand it please...
The problem I'm running into right now is that you haven't stated what you're problem is yet. If you have time I would suggest that you start a new project in a new folder and start again from scratch, I'm trying to change things from a copy paste I did and this might be too far gone to save as is.
I'm working on rewritting your class(es) so that I don't give you the answer, and there should be two by the way. This will allow you to create an individual bus for each object which has an individual object for each passenger.
EDIT: Also by declaring an object of the passenger type you'll be able to associate each passenger with the bus they are on.
I also recommend you just restarting. You have numerous errors (I just looked over the code for an hour now trying to correct it and ended up just starting from scratch). You're better off trying to see if you can retry and get it right the second time.
I recommend you make a struct for bus seats that carry if they are occupied, and who is sitting there.
Here's the peak of my kindness. Its still missing just about everything and I removed stuff for you to put in on your own.
#include <string>
#include <cstdlib>
#include <iostream>
// Simple Bus Seat Struct. Holds all data associated with a bus seat
// Can include who is sitting there or if the seat is still free.
struct Bus_Seat
{
bool isTaken;
std::string owner;
// Constructors.
Bus_Seat()
{
isTaken = false;
owner = "Empty";
}
Bus_Seat(std::string ownerName, bool setTaken = true)
{
owner = ownerName;
isTaken = setTaken;
}
// a simple structure method to instantly clear a seat.
void clear()
{
isTaken = false;
owner = "Empty";
}
};
// The actual class!
class Mass_Transit
{
public: // Methods and Constructors
// Main Constructors
Mass_Transit() { ++BusCreated; this->create_new_route(); }
//Destructor~!
~Mass_Transit() { --BusCreated; }
// Public methods go here!
static std::string vline (char ch); // Static means you can call this method even if you don't have a object of the class.
// So you can call this line from main() without saying bus.vline('*') but rather Mass_Transit::vline('*'); Gives scope to this item.
void create_new_route();
void empty_bus();
void print_bus();
private: // All variables here. Data hiding. You shouldn't be messing with these without the methods.
staticint BusCreated; // Basically defined a scoped Global integer. Anything can edit the value. // I recommend you leave it to only editing in the class via the constructor and destruct or as seen above.
short bus_number;
std::string driver;
short time_arrival_hour, time_arrival_min;
short time_depart_hour, time_depart_min;
std::string start_location,
destination;
Bus_Seat seats[32];
};
// You need to do this with static class variables.
int Mass_Transit::BusCreated = 0;
int main()
{
Mass_Transit bus; // Made a bus.
bus.empty_bus(); // Emptied it.
bus.print_bus(); // Showed the data entered at runtime.
return 0;
}
std::string Mass_Transit:: vline (char ch)
{
return std::string(80, ch);
}
void Mass_Transit:: create_new_route ()
{
std::cout << "For Bus #" << BusCreated << std::endl;
std::cout << "Enter Bus Number : "; std::cin >> bus_number;
std::cin.ignore();
std::cout << "Enter Driver's Name : "; std::getline( std::cin, driver );
std::cout << "Departure Time : "; std::cin >> time_depart_hour >> time_depart_min;
std::cin.ignore();
std::cout << "Arrival Time : "; std::cin >> time_arrival_hour >> time_arrival_min;
std::cin.ignore();
std::cout << "Departing from : "; std::getline( std::cin, start_location );
std::cout << "Destination : "; std::getline( std::cin, destination );
}
void Mass_Transit:: empty_bus ()
{
for (int i = 0; i < 32; ++i)
seats[i].clear();
}
void Mass_Transit:: print_bus ()
{
std::cout << std::endl;
std::cout << vline('*');
std::cout << " Bus No." << bus_number << std::endl;
std::cout << " Driver: " << driver << std::endl;
std::cout << " Departure: " << time_depart_hour << ":" << time_depart_min << std::endl;
std::cout << " Arrival: " << time_arrival_hour << ":" << time_arrival_min << std::endl;
std::cout << " From: " << start_location << std::endl;
std::cout << " To: " << destination << std::endl;
std::cout << vline('*');
}
AARRRGGGHHH!!! I can't believe that on a post containing 101 lines of code I STILL get ninja'd! Seriously though nice work wolfgang.
EDIT: @ OP: Trust me when I say you will want to use headers and linked cpp files for this project.
@wolfgang: The assignment asked for him to print the ticket information at the end which I thought would be easier if the passenger was a class by themself. But your way still works if you add a variable or two.
I'm not trying to do his homework (although it certainly looks that way). I was seriously looking to reorganize the code so he could see what clear coding looks like with some strings. Its up to him program in all his projects needs.
nice one wolfgang. the seats kinda confused me when i looked at it. i was thinking row on the left row on the right 2 seats wide and 20 seats back per side. but you simplified it making it better.
I just thought it was odd to leave that part out(It was also the part I was working on when I saw your post), but you're right he should be able to piece it together himself.
most of his original work was straight functions. i dont think he could turn wolfgangs code in anyway since it obviously uses things above the current classroom leve of what he was studying.