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
|
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
#define MAXNAME 100
#define FILENAME "flights.txt"
typedef struct date{
int day;
int month;
int year;
}d;
typedef struct booking{
char bookingId[10];
char name[20];
d date;
int seatBooked;
double cost;
}b;
typedef struct flightRecord{
int flightId;
char route[8];
int maxSeats;
int seatsAvail;
double costPP;
}f;
string GenerateCode(string , string);
void enterFlightDetails(fstream & file, f*, int);
void updateFlight(fstream & file, f*, int);
void flightReport(fstream & file, f*, int);
int main(int argc, char *argv[])
{
fstream file;
int size =1;
f*fArray = new f[size];
b*bArray = new b[size];
int menuChoice;
cout<<"Enter a choice: \n"<<endl;
cout<<"1. Enter details"<<endl;
cout<<"2. Update flights"<<endl;
cout<<"3. Flight report"<<endl;
cout<<"4. Exit***bring to class***"<<endl;
cout<<"Enter a choice"<<endl;
cin>>menuChoice;
switch(menuChoice)
{
case 1: cin.ignore();
enterFlightDetails(file,fArray,size);
break;
case 2: cin.ignore();
updateFlight(file,fArray,size);
break;
case 3: cin.ignore();
flightReport(file,fArray,size);
break;
case 4: return EXIT_SUCCESS;
break;
default: cout<<"Invalid number, please enter a choice from 1-4"<<endl;
break;
}
system("PAUSE");
return EXIT_SUCCESS;
}
void enterFlightDetails(fstream & file, f*fArray,int size)
{
char flightcodechar[8];
string departure;
string destination;
string fcode;
cout<<"Enter flight details:"<<endl;
f record;
file.open(FILENAME,ios::out |ios::binary);
for(int i=0; i<size; i++)
{
cout<<"FlightID:"<<10000+i<<endl;
record.flightId = 10000+i;
cout<<"Enter departure zone"<<endl;
cin>>departure;
cout<<"Enter destination zone"<<endl;
cin>>destination;
fcode = GenerateCode(departure, destination);
for(int i=0;i<9;i++)
flightcodechar[i]=fcode[i];
//Problem starts here
record.route[8]=flightcodechar[9];
cout<<flightcodechar<<endl;
// record.route=GenerateCode(departure, destination);
cout<<"Enter the max number of seats available"<<endl;
cin>>record.maxSeats;
cin.ignore();
file.write(reinterpret_cast< char *>(&record), sizeof(f));
}
file.close();
flightReport(file,fArray,size);
}
string GenerateCode(string departure, string destination)
{
string flightcode = "";
int i;
for(i = departure.length() - 4; i < departure.length(); i++)
{
flightcode += departure[i];
}
flightcode += "-";
for(i = 0; i <= 2; i++)
{
flightcode += destination[i];
}
return flightcode;
}
void updateFlight(fstream & file, f*fArray,int size)
{
cout<<"UpdateFlight:"<<endl;
}
void flightReport(fstream & file, f*fArray,int size)
{
cout<<"Flight Report:"<<endl;
f record;
file.open(FILENAME,ios::in|ios::binary);
file.clear();
file.seekg (0,ios:: beg);
file.read( reinterpret_cast< char *>(&record ), sizeof(f));
while(!file.eof())
{
cout <<"Flight ID:\t\t"<<record.flightId<<endl;
cout <<"Route:\t\t"<<record.route<<endl;
cout<<"Max seats available:\t"<<record.maxSeats<<endl;
file.read( reinterpret_cast< char *> (&record),sizeof(f));
}
file.close();
}
|