How can I make this program work? Problem with arrays

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

using namespace std;
void details( string fNum,int dHour,int dMin,int aHour,int aMin, string aircraft);

int main()
{
 //Flights tB:Trinidad-Barbados,tN:Trinidad to New York,tL:Trinidad to London.
        int dHour;
        int dMin;
        string fNum;
        int aHour;
        int aMin;
        int durHour;
        int durMin;
        string aircraft;
        int dOption;
        bool availabe=true;
        int seats[max_seat_rows][max_seat_cols]={0};
        int resAmt;
        int empty=0;

//Recording Reservations for Passengers
        cout<<"\nWhat is your destination?\n"<<endl<<"1) Trinidad to Barbados"<<endl
                                               <<"2) Trinidad to New York"<<endl
                                               <<"3) Trinidad to London\n"<<endl;
        cout<<"Please enter either 1,2 or 3."<<endl;
        cin>>dOption;

        switch (dOption)
        {
                case 1:
                        dHour=7;
                        dMin=0;
                        fNum="USC 215";
                        aHour=7;
                        aMin=45;
                        durHour=0;
                        durMin=45;
                        aircraft="MD 82";
                        max_seat_rows=15;
                        max_seat_cols=4;


                break;

                case 2:
                        dHour=6;
                        dMin=0;
                        fNum="USC 400";
                        aHour=3;
                        aMin=0;
                        durHour=6;
                        durMin=0;
                        aircraft="Boeing 737";
                        max_seat_rows=32;
                        max_seat_cols=6;
                break;

                case 3:
                        dHour=7;
                        dMin=30;
                        fNum="USC 900";
                        aHour=9;
                        aMin=30;
                        durHour=9;
                        durMin=0;
                        aircraft="Boeing 777";
                        max_seat_rows=30;
                        max_seat_cols=10;
                break;
        }


                details(fNum,dHour,dMin,aHour,aMin, aircraft);
//This section declares everything in all of the arrays to 0;


                 for(int a=0;a<max_seat_rows;a++)
                 {
                        for(int b=0;b<max_seat_cols;b++)
                        {
                                cout<<seats[a][b]<<"\t";

                        }
                        cout<<endl;

                 }

// This section determines how many empty seats there are

                 for(int c=0;c<max_seat_rows;c++)
                 {
                        for(int d=0;d<max_seat_cols;d++)
                        {
                                if (seats[c][d]==0)
                                empty++;

                        }
                 }

                        cout<<"There are "<<empty<<" empty seats"<<endl;
//This section takes the amount of seats that the user wishes to reserve and makes them unavailable


                        cout<<"How many seats would you like to reserve?"<<endl;
                        cin>>resAmt;

                        for (int e=0;e>resAmt;e++)
                        {

                                        for(int c=0;c<max_seat_rows;c++)
                                        {
                                                for(int d=0;d<max_seat_cols;d++)
                                                {
                                                        if (seats[c][d]==0)
                                                        {
                                                        seats[c][d]=1;
                                                        }
                                                        else
                                                        {
                                                        cout<<"This seat is already taken"<<endl;
                                                        }

                                                }
                                        }


                        }

return 0;


}


               void details( string fNum,int dHour,int dMin,int aHour,int aMin, string aircraft)
{
                cout<<"\n\nYou Have Chosen to Fly on Flight Number "<<fNum<<endl;
                cout<<"The Flight will Depart at "<<dHour<<":"<<dMin<<endl;
                cout<<"The Flight will Arrive at its Destination in "<<aHour<<" Hours and "<<aMin<<" Minutes"<<endl;
                cout<<"Aircraft is the "<<aircraft<<endl;

}
What are the problems?
hmm... I'm not a real programmer (a lot of people can help you better than me) however I can give you a suggestion and underline a great error in the concept of your source code.

Suggestion:
You are trying to create a sort of Database of airplanes, anyone with this own places, name and structure, but all with similar structure.
In this case, instead of using a lot of parameters in main, it is better to use objects for example:

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
class DayTime {
    int _hour;
    int _min;
  public:
    void setTime(int, int);
    int hour() const;
    int min() const;
};

void DayTime::setTime(int a, int b) {
  _hour = a;
  _min = a;
}

int DayTime::hour() const {
  return _hour;
}

int DayTime::min() const {
  return _min;
}

//-------

class Places {
     std::vector <int> _place;
     int _rows;
     int _columns;
  public:
     Places(int, int);
     ~Places();
     int getPlace(int, int) const;
     void setPlace(int, int, int);
};

Places::Places (int rows, int columns) {
  _rows = rows; _colums = columns;
  if(_rows <= 0) _rows = 1; //at least 1 row
  if(_colums <=0) _colums = 1; //at least 1 column
  for (int a = 0; a < (_rows * _columns); a++) _place.push_back(0); //set single place to 0
}

Places::~Places() {
  _place.clear(); //delete std::vector at destruction
}

int getPlace(int row, int colum) const {
  if(row > (_rows-1) || colum > (_colums -1) ) return -1; //return -1 if row or colums is out of size
  int index = row + (colum * _rows);
  return _place[index];
}

void setPlace(int row, int colum, int value) {
  if(row < _rows && colum <_colums) { //only if row and column under limits
     int index = row + (colum * _rows);
     _place[index] = value;
  }
}

//-------

class Fly {
     DayTime _leaveAt;
     DayTime _travelTime;
     std::string _fNum;
     std::string _aircraft;
     Places *places;
     ...
}

etc


My example can contain errors or can be bad (not tested and I'm not a programmer, as I said) but however can show you some things.
First of all I use a class DayTime for time of fly (this can be useless) so you can have only a var for a time (duration and time of leaving). With objects you can add other features (like showing DayTime as "hh:mm" or other nice things).
The second class is most important: Places.
Now I explain your greatest error in your source code;
you declared an array int place [var] [var]; with an unitialized var. This is an error. This is not a way to decide the size of an array. I suggest you to read something about dinamic memory allocation.

In my example I created a class Place.
The class place use a std::vector (but seeing my example, I could simply use a pointer and the "new" kwyowrd in that case.... I used std::vector only to suggest you also to take a look at std::vector if you don't know it).
Place must be initialized with number of rows and colums. Once a Place object is created it cannot be resized (seeing the fact that an airplain model have always the same places). if you want to be allowed to resize object place you have to change compleately the code.
In my example place class contains an internal buffer (_place) that contains all places. Once created _place is called like a one-dimension array (I didn't use a bidimensional dinamic allocation becouse it is too complex for simply example). In this way the internal buffer has only one index. But, however, the user of the class can find the place setting the row and the colum where the place is located. With getPlace(int, int) you can obtain the int value of the place (x, y) [x and y are both from 0 to total-1]. with setPlace(int, int, int value) you can set place (x, y) to the value "value".
the class calculates internally the internal index using row and colum as parameters and returns (or modify) the relative value.

in the class fly, as you said, Place is defined as a pointer. Infact you have to inizialize it as a pointer.
Example

 
places = new Places (5,4); //create a new object Places with 5 rows and 4 colums 


and you have to remember to delete it on destructor

--------

however take my code only as example to understand what I'm trying to explain to you. Probably my code can be not correct and surely can be written better. It is only a try to explain your error and how you can use objects in your project
Topic archived. No new replies allowed.