bus reservation system

This is what I have so far. It has no errors but has trouble running. The project must include
1. the Adding and Removal of records,
2. the Modification of records,
3. the printing of reports
The system will need to have the following
1. arrays
2. functions
3. files

Any help?

#include <conio.h>

#include <cstdio>

#include <iostream>

#include <string.h>

#include <cstdlib>

using namespace std;

static int p = 0;

class a

{

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 avail();

void position(int i);

}

bus[10];

void vline(char ch)

{

for (int i = 80; i>0; i--)

cout << ch;

}

void a::install()

{

cout << "Enter bus no: ";

cin >> bus[p].busn;

cout << "\nEnter Driver's name: ";

cin >> bus[p].driver;

cout << "\nArrival time: ";

cin >> bus[p].arrival;

cout << "\nDeparture: ";

cin >> bus[p].depart;

cout << "\nFrom: \t\t\t";

cin >> bus[p].from;

cout << "\nTo: \t\t\t";

cin >> bus[p].to;

bus[p].empty();

p++;

}

void a::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 << "\nSeat Number: ";

cin >> seat;

if (seat>32)

{

cout << "\nThere 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 a::empty()

{

for (int i = 0; i<8; i++)

{

for (int j = 0; j<4; j++)

{

strcpy_s(bus[p].seat[i][j], "Empty");

}

}

}

void a::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

<< "\nDriver: \t" << bus[n].driver << "\t\tArrival time: \t"

<< bus[n].arrival << "\tDeparture time:" << bus[n].depart

<< "\nFrom: \t\t" << bus[n].from << "\t\tTo: \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 << "\nThe seat no " << (a - 1) << " is reserved for " << bus[n].seat[i][j] << ".";

}

}

break;

}

if (n>p)

cout << "Enter correct bus no: ";

}

void a::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\nThere are " << p << " seats empty in Bus No: " << bus[l].busn;

}

void a::avail()

{

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 << "\nFrom: \t\t" << bus[n].from << "\t\tTo: \t\t\t"

<< bus[n].to << "\n";

vline('*');

vline('_');

}

}

int main()

{

system("cls");

int w;

while (1)

{

//system("cls");

cout << "\n\n\n\n\n";

cout << "\t\t\t1.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\tEnter 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].avail();

break;

case 5: exit(0);

}

}

return 0;

}
Your code is unreadeable without code tags and reasonable indentation.

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/
Hint: You can edit your post, highlight your code and press the <> formatting button.
I agree on the code tags! Not hard to do, makes it a lot easier to see what's going on.

You've worked hard on this, I can see. But your implementation has boxed you into some absolutes - you only allow for a maximum of 10 buses, and with them 10 drivers (don't have to be unique), etc. . .Have you ever used or learned about vectors?

Also: you #include <string> but all your string-like variables are old-style array[]. Couldn't most of those be strings?

Also:

Assigning p as a static global variable for what should be a single-use value (number of buses) makes it very unwise to use p as a counter, pointer, etc. for any other part of the program. If you need to keep a static count of your buses, use something distinctive, like . . . static int buscount; so you always know when you're accessing that value, and more importantly, when you're altering it. For example:

1
2
3
4
5
void a::position(int l)
           {
                 int s = 0; p = 0; //since p is global the semicolon before p doesn't cause an error
                              //but it resets your bus count to zero.
                              //later it gets increased to the seat capacity of the bus (32). 


I'm not a big fan of non-descriptive variables either: in that code fragment, what is being received by the method in variable (int l)? What are int s = 0, p = 0 going to do in the method? If you have a small loop using a for( i = 0; i < variable ; i++) it's obvious, no problem. But when the declared variable is way down inside several code blocks. . .hard to make it understandable.
Last edited on
Topic archived. No new replies allowed.