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
|
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
#define N 10
using namespace std;
//Prototype functions
int printBookings(int[], int);
double Income(int[], int, double);
int empty(int[], int);
int main()
{
//Declaring and opening the file
/************************************************/
ifstream flightInfo;
flightInfo.open("flightInfo.txt");
if(!flightInfo.is_open())
cout << "File did NOT open correctly";
/***********************************************/
cout.setf(ios::fixed, ios::floatfield); // used to set width/spacing
int bookings[N];
int trans, fn, seats, emptyFlight;
double totalIncome;
for(int i=0; i<N; i++)
bookings[i]=0; // Initializes all elements to 0
flightInfo >> trans >> fn >> seats; // Reads all three lines of file
while(flightInfo){
if(trans==1){
bookings[fn]+=seats;
cout << "Seats added";}
if(trans==2 && bookings[fn]>seats)
bookings[fn]-=seats; // only executes if seats currently booked are greater than being canceled
else if(trans==2 && bookings[fn]<seats)
bookings[fn]=bookings[fn]; // if seats canceled exceed currently booked, bookings stay the same.
flightInfo >> trans >> fn >> seats;
}
printBookings(bookings, N);
emptyFlight = empty(bookings, N);
totalIncome = Income(bookings, N, 120.00);
cout << "\n\nThe number of flights with zero bookings are: " << emptyFlight << endl;
cout << "The total income generated by ticket sales today was: " << setprecision(2) << totalIncome << "\n\n\n\n";
return 0;
}
int printBookings(int a[], int elements){
cout << "Flight #" << "\tBookings" << "\tMessage Log\n";
for(int i=0; i<elements; i++)
cout << setw(5) << i << setw(15) << a[i] << endl; // Prints chart showing flight # alongside seats booked
}
double Income(int a[], int elements, double seatPrice){
int seatsUsed=0;
double income;
for(int i=0; i<elements; i++){
if(a[i]>0)
seatsUsed+=a[i]; // calculates sum of all booked seats
}
income = seatsUsed*seatPrice;
return income;
}
int empty(int a[], int elements){
int cnt0 = 0;
for(int i=0; i<elements; i++)
if(a[i]==0) // If zero seats are booked for a flight
cnt0++;
return cnt0;
}
|