Printing Message log for Flight transaction

I have an assignment involving parallel arrays in which I have to update data of flight transactions. Im almost done with it, except the message log is not printing out correctly.

To explain briefly:

We were given two files: One with 3-digit flight numbers for 10 flights, and another with the transactions involving those flights.

there are two transaction types: 1 and 2
type 1 is booking seats
type 2 is cancelling seats

the code is supposed to match the flight number with array position number using parallel arrays. If it does not find the flight number in the file, it prints "Invalid flight number" onto the screen.

However, my code seems to be printing this message for ALL transactions, even in the ones that have valid flight numbers.

What am I doing wrong?
[Ignore the sortByCount and sortByFlight functions, they are irrelevant in this discussion]


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
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#define N 10

using namespace std;

//Prototype functions
void initialize(int a[], int b[]);
void printBookings(int a[], int b[], int);
int findFlight(int a[], int , int fn);
void sortByFlight(int a[], int b[], int );
void sortByCount(int a[], int b[], int );


// Declaring the files
ifstream flightNumber;
ifstream transactions;


int main()
{
//Opening the flightNumbers file
/************************************************/
    flightNumber.open("flightNumber.txt");

    if(!flightNumber.is_open())
    {
        cout << "File 1 did NOT open correctly";
        return 0;
    }
/***********************************************/

//Opening the transactions file
/************************************************/
    transactions.open("transactions.txt");

    if(!transactions.is_open())
    {
        cout << "File 2 did NOT open correctly";
        return 0;
    }
/***********************************************/


cout.setf(ios::fixed, ios::floatfield); // used to set width/spacing

int flights[N];
int bookings[N];

initialize(flights, bookings);
printBookings(flights, bookings, N);

int trans, fn, seats;
int pos;


cout << "\n\n\nData to be Processed:\n\n";
cout << "Transaction Type" << setw(13) << "Flight #" << setw(15) << "# of Seats" << setw(17) << "Log\n\n";


transactions >> trans >> fn >> seats; // Reads first line of transactions file
while(transactions)
{
pos = findFlight(flights, N, fn);

if(pos == -1)
    cout << setw(8) << trans << setw(17) << fn << setw(15) << seats << setw(32) << "Invalid flight number" << endl;

else if(pos > -1)
{
    if(trans==1)
        {
            {
                bookings[pos]+=seats; // Add seats to flight
                cout << setw(8) << trans << setw(17) << fn << setw(15) << seats << setw(10) << seats << " seats booked on flight " << fn << endl;
            }
        }

    if(trans==2)
        {
            if(bookings[pos]>=seats) // if number of seats booked exceed seats to be cancelled
                {
                    bookings[pos]-=seats; // cancel seats from flight
                    cout << setw(8) << trans << setw(17) << fn << setw(15) << seats << setw(32) << seats << " seats cancelled from flight " << fn << endl;
                }
            else cout << setw(8) << trans << setw(17) << fn << setw(15) << seats << setw(32) << seats << " seats were not cancelled" << endl;

        }

else if (trans!=1 && trans!=2)
    cout << setw(8) << trans << setw(17) << fn << setw(15) << seats << setw(32) << "Invalid transaction type"  << endl;
}



    transactions >> trans >> fn >> seats; // reads next line of data
}

cout << "\n\n\nFinal Bookings:\n\n";
printBookings(flights, bookings, N); // Prints the final chart, with seats booked for each flight

sortByFlight(flights, bookings, N); // Sorts by flight # order (increasing)
printBookings(flights, bookings, N); // Prints in flight # order

sortByCount(flights, bookings, N); // Sorts by bookings order (decreasing)
printBookings(flights, bookings, N); // Prints in bookings order


}



void initialize(int a[], int b[])
{
    int fn;
    int i = 0;

    flightNumber >> fn;
    while(flightNumber)
    {
        a[i] = fn;
        i++;

        flightNumber >> fn;
    }

     for(int n=0; n<N; n++)
        b[n]=0;
}



void printBookings(int a[], int b[], int )
{

cout << "Flight #" << "\tBookings\n\n";

for(int i=0; i<N; i++)
    {
        cout << setw(5) << a[i] << setw(15) << b[i] << endl;
    }
}



int findFlight(int a[], int , int fn)
{
    for(int pos = 0; pos<N; pos++)
        if(a[pos]==fn)
            return pos;
        else return -1;
}



void sortByFlight(int a[], int b[], int)
{
    int temp1, temp2;

    for(int pass=1; pass<=N-1; pass++)
        for(int i=0; i<N-1; i++)
        if(a[i]>a[i+1])
        {
            temp1 = a[i];
            a[i] = a[i+1];
            a[i+1]=temp1;

            temp2 = b[i];
            b[i] = b[i+1];
            b[i+1] = temp2;
        }
}



void sortByCount(int a[], int b[], int)
{
    int temp1, temp2;

    for(int pass=1; pass<=N-1; pass++)
        for(int i=0; i<N-1; i++)
        if(b[i]<b[i+1])
        {
            temp1 = b[i];
            b[i] = b[i+1];
            b[i+1]=temp1;

            temp2 = a[i];
            a[i] = a[i+1];
            a[i+1] = temp2;
        }

}
Last edited on
Anyone?

Topic archived. No new replies allowed.