Hi again, I made a program that uses parallel arrays to print out flight numbers and transactions.
My problem is that when i compile it, it shows no errors, when i run it, the output window crashes.
here is my program:
Code:#include <iostream> #include <cmath> #include <fstream> using namespace std; int initialize(int [], int[], int); void printBookings(int [], int [], int); int findFlight( int [], int, int); int main() { ifstream trans; trans.open("trans.dat"); int flightNo[20]; int bookings[20]; int read, type, flight, seat,number,i; read = 0; type = 0; flight = 0; seat = 0; initialize(flightNo, bookings, 20); printBookings(flightNo, bookings, 20); trans >> type >> flight>> seat; cout <<"Transactions"<<endl; while( type!= -1) { if( type == 1) { if( findFlight(flightNo, 20, flight) != -1) { bookings[findFlight(flightNo, 20, flight)]+= seat; cout<< seat<< " "<< "seats booked on flight"<<" "<< flight<<endl; } else if( findFlight(flightNo, 20, flight) == -1) cout<<"Invalid Flight Number"<<endl; } else { if(type == 2) { if(seat < bookings[findFlight(flightNo, 20, flight)]) { bookings[findFlight(flightNo, 20, flight)]-= seat; cout<< seat<<" "<<"seats cancelled from flight"<<" "<<flight<<endl; } else if(seat > bookings[findFlight(flightNo, 20, flight)]) { seat-= bookings[findFlight(flightNo, 20, flight)]; cout<<seat<< " "<<"seats were not cancelled"<<endl; } } else cout<<"Invalid Transaction Type"<<endl; } trans >> type >> flight>> seat; } printBookings(flightNo, bookings, 20); system("pause"); return 0; } int initialize(int flightNo[], int book[], int count) { int read= 0; int i,number; ifstream flightnum; flightnum.open("flightnum.dat"); flightnum>>number; while(number != -1) { flightNo[i] = number; flightnum>>number; read++; } for( i=0; i<count; i++) { book[i] = 0; read++; } return read; } void printBookings(int flightNo[], int bookings[], int count) { int i; cout << "Flight Numbers"<< " "<<"Current Bookings"<< endl; for( i =1; i<count; i++) cout << flightNo[i] << " "<< bookings[i]<<endl; } int findFlight( int flightNo[], int count, int num) { int i, index=0; for(i = 0; i<count; i++) { if( flightNo[i] == num) return index; index++; } return -1; }
it reads from 2 files:
1 157 200
2 302 150
1 673 300
1 211 50
1 358 100
3 673 10
2 358 400
2 673 250
5 412 150
1 266 40
3 409 0
1 673 75
1 358 500
3 412 0
2 157 400
1 409 300
2 358 200
-1 -1 -1
and
511
358
302
412
208
673
157
129
266
409
-1
i dont know why my screen keeps crashing



LinkBack URL
About LinkBacks


