Writing a Code on Bus Ticket system...

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.





#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
static int p=0;
class atrans
{
char busn[5],driver[10],arrival[5],depart[5],from[10],to[10],
seat[8][4][10];
public:
void install();
void allotment();
void empty();
void show();
void available();
void position(int i);
}bus[5];//here we declare the number of buses we can have.
void vline(char ch)
{
for (int i=80;i>0;i--)//Here i's value will depend on your computer.
cout<<ch;
}
void atrans::install()
{
cout<<"Enter bus no: ";
cin>>bus[p].busn;
cout<<"\n Enter Driver's name: ";
cin>>bus[p].driver;
cout<<"\n Departure: ";
cin>>bus[p].depart;
cout<<"\n Arrival time: ";
cin>>bus[p].arrival;
cout<<"\n From: \t\t\t";
cin>>bus[p].from;
cout<<"\n To: \t\t\t";
cin>>bus[p].to;
bus[p].empty();
p++;
}
void atrans::allotment()
{
int seat;
char number[5];
top:
cout<<"Bus no: ";
cin>>number;
int n;
for(n=0;n<=p;n++)
{
if(strcmp(bus[n].busn,number)==0)
break;
}
while(n<=p)
{
cout<<"\n Seat number: ";
cin>>seat;
if (seat>32)
{
cout<<"\n There are only 32 seats available in this bus.";
}
else
{
if (strcmp(bus[n].seat[seat/4][(seat%4)-1],"Empty")==0)
{
cout<<"Enter passanger's name: ";
cin>>bus[n].seat[seat/4][(seat%4)-1];
break;
}
else
cout<<"The seat no. is already reserved.\n";
}
}
if (n>p)
{
cout<<"Enter correct bus no.\n";
goto top;
}
}
void atrans::empty()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<4;j++)
{
strcpy(bus[p].seat[i][j],"Empty");
}
}
}
void atrans::show()
{
int n;
char number[5];
cout<<"Enter bus no: ";
cin>>number;
for(n=0;n<=p;n++)
{
if(strcmp(bus[n].busn,number)==0)
break;
}
while (n<=p)
{
vline('*');
cout<<" Bus no: \t"<<bus[n].busn
<<"\n Driver: \t"<<bus[n].driver<<"\t\t Departure time:\t"
<<bus[n].depart<<"\t Arrival time:\t"<<bus[n].arrival
<<"\n From:\t Melaka Station\t"<<bus[n].from<<"\t\t To: \t\t\t"<<
bus[n].to<<"\n";
vline('*');
bus[0].position(n);
int a=1;
for (int i=0;i<8;i++)
{
for(int j=0;j<4;j++)
{
a++;
if(strcmp(bus[n].seat[i][j],"Empty")!=0)
cout<<"\n The seat no "<<a-1<<" is reserved for "<<bus[n].seat[i][j]<<" .";
}
}break;
} if(n>p)
cout<<"enter correct bus no.";
}
void atrans::position(int l)
{
int s=0,p=0;
for(int i=0;i<8;i++)
{
cout<<"\n";
for(int j=0;j<4;j++)
{
s++;
if(strcmp(bus[l].seat[i][j],"Empty")==0)
{
cout.width(5);
cout.fill(' ');
cout<<s<<".";
cout.width(10);
cout.fill(' ');
cout<<bus[l].seat[i][j];
p++;
}
else
{
cout.width(5);
cout.fill(' ');
cout<<s<<".";
cout.width(10);
cout.fill(' ');
cout<<bus[l].seat[i][j];
}
}
}
cout<<"\n\n There are "<<p<<" seats empty in Bus No: "<<bus[l].busn;
}
void atrans::available()
{
for(int n=0;n<p;n++)
{
vline('*');
cout<<"Bus no: \t"<<bus[n].busn<<"\nDriver: \t"<<bus[n].driver<<"\t\tArrival time:\t"<<bus[n].arrival<<"\tDeparture Time: \t"<<bus[n].depart<<"\n From: \t\t"<<bus[n].from<<"\t\t To: \t\t\t"<<bus[n].to<<"\n";
vline('*');
vline('_');
}
}
int main()
{
//clrscr();
int w;
//int gd=DETECT,gm;
//initgraph(&gd,&gm,"d:\\tc\\bgi");//enter the path of ur c compiler where u installed it.
//setbkcolor(GREEN);
while(1)
{
cout<<"*****************************************************"<<endl;
cout<<"******* *******"<<endl;
cout<<"* The Administrator *"<<endl;
cout<<"* Melaka Transport Service *"<<endl;
cout<<"******* *******"<<endl;
cout<<"*****************************************************"<<endl;

cout<<"\t\t\t 1.Install\n\t\t\t 2.Reservation\n\t\t\t 3.Show \n\t\t\t 4.Buses Available. \n\t\t\t 5.Exit";
cout<<"\n\t\t\t Enter your choice:-> ";
cin>>w;
switch(w)
{
case 1:
bus[p].install();
break;
case 2:
bus[p].allotment();
break;
case 3:
bus[0].show();
break;
case 4:
bus[0].available();
break;
case 5:
exit(0);
}
}
}
Oh god. Please use [code] tags. Edit your post and then format the text with proper indentation.

Please also tell me this:
* Can you use std::string?
* Can you use std::vector?
* Does this have to be a class?
How do i use code tag??

well...can use any..but familiar with class...
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
static int p=0;
class atrans{
char busn[5],driver[10],arrival[5],depart[5],from[10],to[10],
seat[8][4][10];
public:
void install();
void allotment();
void empty();
void show();
void available();
void position(int i);
}bus[5];//here we declare the number of buses we can have.
void vline(char ch){
for (int i=80;i>0;i--)//Here i's value will depend on your computer.
cout<<ch;
}
void atrans::install(){
cout<<"Enter bus no: ";
cin>>bus[p].busn;
cout<<"\n Enter Driver's name: ";
cin>>bus[p].driver;
cout<<"\n Departure: ";
cin>>bus[p].depart;
cout<<"\n Arrival time: ";
cin>>bus[p].arrival;
cout<<"\n From: \t\t\t";
cin>>bus[p].from;
cout<<"\n To: \t\t\t";
cin>>bus[p].to;
bus[p].empty();
p++;
}
void atrans::allotment(){
int seat;
char number[5];
top:
cout<<"Bus no: ";
cin>>number;
int n;
for(n=0;n<=p;n++){
if(strcmp(bus[n].busn,number)==0)
break;
}
while(n<=p){
cout<<"\n Seat number: ";
cin>>seat;
if (seat>32)
{
cout<<"\n There are only 32 seats available in this bus.";
}
else
{
if (strcmp(bus[n].seat[seat/4][(seat%4)-1],"Empty")==0)
{
cout<<"Enter passanger's name: ";
cin>>bus[n].seat[seat/4][(seat%4)-1];
break;
}
else
cout<<"The seat no. is already reserved.\n";
}
}
if (n>p){
cout<<"Enter correct bus no.\n";
goto top;
}
}
void atrans::empty(){
for(int i=0;i<8;i++){
for(int j=0;j<4;j++){
strcpy(bus[p].seat[i][j],"Empty");
}
}
}
void atrans::show(){
int n;
char number[5];
cout<<"Enter bus no: ";
cin>>number;
for(n=0;n<=p;n++){
if(strcmp(bus[n].busn,number)==0)
break;
}
while (n<=p){
vline('*');
cout<<" Bus no: \t"<<bus[n].busn
<<"\n Driver: \t"<<bus[n].driver<<"\t\t Departure time:\t"
<<bus[n].depart<<"\t Arrival time:\t"<<bus[n].arrival
<<"\n From:\t Melaka Station\t"<<bus[n].from<<"\t\t To: \t\t\t"<<
bus[n].to<<"\n";
vline('*');
bus[0].position(n);
int a=1;
for (int i=0;i<8;i++){
for(int j=0;j<4;j++){
a++;
if(strcmp(bus[n].seat[i][j],"Empty")!=0)
cout<<"\n The seat no "<<a-1<<" is reserved for "<<bus[n].seat[i][j]<<" .";
}
}break;
} if(n>p)
cout<<"enter correct bus no.";
}
void atrans::position(int l){
int s=0,p=0;
for(int i=0;i<8;i++){
cout<<"\n";
for(int j=0;j<4;j++){
s++;
if(strcmp(bus[l].seat[i][j],"Empty")==0)
{
cout.width(5);
cout.fill(' ');
cout<<s<<".";
cout.width(10);
cout.fill(' ');
cout<<bus[l].seat[i][j];
p++;
}
else
{
cout.width(5);
cout.fill(' ');
cout<<s<<".";
cout.width(10);
cout.fill(' ');
cout<<bus[l].seat[i][j];
}
}
}
cout<<"\n\n There are "<<p<<" seats empty in Bus No: "<<bus[l].busn;
}
void atrans::available()
{
for(int n=0;n<p;n++){
vline('*');
cout<<"Bus no: \t"<<bus[n].busn<<"\nDriver: \t"<<bus[n].driver<<"\t\tArrival time:\t"<<bus[n].arrival<<"\tDeparture Time: \t"<<bus[n].depart<<"\n From: \t\t"<<bus[n].from<<"\t\t To: \t\t\t"<<bus[n].to<<"\n";
vline('*');
vline('_');
}
}
int main()
{
//clrscr();
int w;
//int gd=DETECT,gm;
//initgraph(&gd,&gm,"d:\\tc\\bgi");//enter the path of ur c compiler where u installed it.
//setbkcolor(GREEN);
while(1){
cout<<"*****************************************************"<<endl;
cout<<"******* *******"<<endl;
cout<<"* The Administrator *"<<endl;
cout<<"* Melaka Transport Service *"<<endl;
cout<<"******* *******"<<endl;
cout<<"*****************************************************"<<endl;

cout<<"\t\t\t 1.Install\n\t\t\t 2.Reservation\n\t\t\t 3.Show \n\t\t\t 4.Buses Available. \n\t\t\t 5.Exit";
cout<<"\n\t\t\t Enter your choice:-> ";
cin>>w;
switch(w){
case 1:
bus[p].install();
break;
case 2:
bus[p].allotment();
break;
case 3:
bus[0].show();
break;
case 4:
bus[0].available();
break;
case 5:
exit(0);
}
}
}
Here I'll help you out since this is probably the least of your issues with this app:

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
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
static int p=0;
class atrans{
char busn[5],driver[10],arrival[5],depart[5],from[10],to[10],
seat[8][4][10];
public:
void install();
void allotment();
void empty();
void show();
void available();
void position(int i);
}bus[5];//here we declare the number of buses we can have.
void vline(char ch){
for (int i=80;i>0;i--)//Here i's value will depend on your computer.
cout<<ch;
}
void atrans::install(){
cout<<"Enter bus no: ";
cin>>bus[p].busn;
cout<<"\n Enter Driver's name: ";
cin>>bus[p].driver;
cout<<"\n Departure: ";
cin>>bus[p].depart;
cout<<"\n Arrival time: ";
cin>>bus[p].arrival;
cout<<"\n From: \t\t\t";
cin>>bus[p].from;
cout<<"\n To: \t\t\t";
cin>>bus[p].to;
bus[p].empty();
p++;
}
void atrans::allotment(){
int seat;
char number[5];
top:
cout<<"Bus no: ";
cin>>number;
int n;
for(n=0;n<=p;n++){
if(strcmp(bus[n].busn,number)==0)
break;
}
while(n<=p){
cout<<"\n Seat number: ";
cin>>seat;
if (seat>32)
{
cout<<"\n There are only 32 seats available in this bus.";
}
else
{
if (strcmp(bus[n].seat[seat/4][(seat%4)-1],"Empty")==0)
{
cout<<"Enter passanger's name: ";
cin>>bus[n].seat[seat/4][(seat%4)-1];
break;
}
else
cout<<"The seat no. is already reserved.\n";
}
}
if (n>p){
cout<<"Enter correct bus no.\n";
goto top;
}
}
void atrans::empty(){
for(int i=0;i<8;i++){
for(int j=0;j<4;j++){
strcpy(bus[p].seat[i][j],"Empty");
}
}
}
void atrans::show(){
int n;
char number[5];
cout<<"Enter bus no: ";
cin>>number;
for(n=0;n<=p;n++){
if(strcmp(bus[n].busn,number)==0)
break;
}
while (n<=p){
vline('*');
cout<<" Bus no: \t"<<bus[n].busn
<<"\n Driver: \t"<<bus[n].driver<<"\t\t Departure time:\t"
<<bus[n].depart<<"\t Arrival time:\t"<<bus[n].arrival
<<"\n From:\t Melaka Station\t"<<bus[n].from<<"\t\t To: \t\t\t"<<
bus[n].to<<"\n";
vline('*');
bus[0].position(n);
int a=1;
for (int i=0;i<8;i++){
for(int j=0;j<4;j++){
a++;
if(strcmp(bus[n].seat[i][j],"Empty")!=0)
cout<<"\n The seat no "<<a-1<<" is reserved for "<<bus[n].seat[i][j]<<" .";
}
}break;
} if(n>p)
cout<<"enter correct bus no.";
}
void atrans::position(int l){
int s=0,p=0;
for(int i=0;i<8;i++){
cout<<"\n";
for(int j=0;j<4;j++){
s++;
if(strcmp(bus[l].seat[i][j],"Empty")==0)
{
cout.width(5);
cout.fill(' ');
cout<<s<<".";
cout.width(10);
cout.fill(' ');
cout<<bus[l].seat[i][j];
p++;
}
else
{
cout.width(5);
cout.fill(' ');
cout<<s<<".";
cout.width(10);
cout.fill(' ');
cout<<bus[l].seat[i][j];
}
}
}
cout<<"\n\n There are "<<p<<" seats empty in Bus No: "<<bus[l].busn;
}
void atrans::available()
{
for(int n=0;n<p;n++){
vline('*');
cout<<"Bus no: \t"<<bus[n].busn<<"\nDriver: \t"<<bus[n].driver<<"\t\tArrival time:\t"<<bus[n].arrival<<"\tDeparture Time: \t"<<bus[n].depart<<"\n From: \t\t"<<bus[n].from<<"\t\t To: \t\t\t"<<bus[n].to<<"\n";
vline('*');
vline('_');
}
}
int main()
{
//clrscr();
int w;
//int gd=DETECT,gm;
//initgraph(&gd,&gm,"d:\\tc\\bgi");//enter the path of ur c compiler where u installed it.
//setbkcolor(GREEN);
while(1){
cout<<"*****************************************************"<<endl;
cout<<"******* *******"<<endl;
cout<<"* The Administrator *"<<endl;
cout<<"* Melaka Transport Service *"<<endl;
cout<<"******* *******"<<endl;
cout<<"*****************************************************"<<endl;

cout<<"\t\t\t 1.Install\n\t\t\t 2.Reservation\n\t\t\t 3.Show \n\t\t\t 4.Buses Available. \n\t\t\t 5.Exit";
cout<<"\n\t\t\t Enter your choice:-> ";
cin>>w;
switch(w){
case 1:
bus[p].install();
break;
case 2:
bus[p].allotment();
break;
case 3:
bus[0].show();
break;
case 4:
bus[0].available();
break;
case 5:
exit(0);
}
}
}
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.

- What are you trying to accomplish with line 38?

- What is line 39 doing in this program?
Last edited on
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...
Thanks Computergeek.

Please can u fix the problem for me and comment,so i will know....also please do read the question again and see if am doing the right thing...
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.
Last edited 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.

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
#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.
    static int      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('*');
}


EDIT: Updated with comments.
Last edited on
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.
Last edited on
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.
Thanks everyone..I will work on this....
@wolfgang....i was unable to comprehend your coding...and also can u use some comment,so i can know what u trying to do...
Edited post above with comments. Any specific questions?
Topic archived. No new replies allowed.