But basically I want to write an array of complex objects to a file to store them and then be able to load the saved data into the arrays again once the program is ran again after exit.
Somebody suggested just storing the data in a text file using XML, and then parsing the text file using the tags stored in the text file.
I was just wondering what the most efficient and best way would be to store the data.
As for a bit of background on the project, the project is an airport booking system which consists of 2 airplanes which hold 24 passengers. The planes can have multiple flights and upto 24 passengers. So passengers must be assigned to flight seats on the planes. All of this data is stored in two objects, one called passengers which has an array size of 24 (currently, I wish to turn this into a vector, but I have not used vectors yet) and flights with an array size of 100 (which I also wish to turn into a vector).
I hope this is enough information for you guys to get an idea. I just want to know the best way to go about it, so that I know from the start, rather than finding out down the line and realising that my way is deprecated or incredibly inefficient.
main.cpp:
//
// main.cpp
// Scotia Airlines
//
// Created by Conner McCabe on 25/11/2012.
// Copyright (c) 2012 Conner McCabe. All rights reserved.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "flight.h"
#include <string.h>
using namespace std;
void mainMenu()
{
char choice[2];
int intChoice;
string flightID;
char seatID[2];
passenger passengers[24];
flight flights[100];
int passengerID;
int first = 0;
/* This loop is used to make sure that the user cannot enter bad data */
do
{
if(first != 1){for(int i=0; i <= 100; i++){cout << endl;}first = 1;}
cout << "\nWelcome to the Scotia Airlines booking system developed by Conner McCabe. \n";
cout << "-----------------------------------------------------------\n";
cout << "* *\n";
cout << "* \\____ ____ *\n";
cout << "* \\ \\ \\ \\ *\n";
cout << "* \\\RMA\\_____ \\ \\ *\n";
cout << "* \\...~-__()______\\___\\_____________________ *\n";
cout << "* \\ Rocky Mountain Airlines ___\\ *\n";
cout << "* \\ oo ooooooooooooooooooooo o o |_O__\\_ *\n";
cout << "* ~~--_________/~~~/________________________) *\n";
cout << "* | / /() | *\n";
cout << "* 0 / /() 0 *\n";
cout << "* ---- *\n";
cout << "* *\n";
cout << "-----------------------------------------------------------\n";
cout << "* Please make a selection from one of the options below: *\n";
cout << "-----------------------------------------------------------\n";
cout << "* *\n";
cout << "* 1. New Passenger *\n";
cout << "* *\n";
cout << "* 2. View Passenger *\n";
cout << "* *\n";
cout << "* 3. New Booking *\n";
cout << "* *\n";
cout << "* 4. New Reservation *\n";
cout << "* *\n";
cout << "* 5. View Availability *\n";
cout << "* *\n";
cout << "* 6. Exit the Program *\n";
cout << "* *\n";
cout << "* *\n";
cout << "-----------------------------------------------------------\n";
cout << "\nPlease enter a number from 1 - 3 to select an option from the main menu: ";
/* This resets the choice for each loop back */
if(choice[0] != 0)
{
choice[0] = 0;
}
/* This statement reads in the user's choice */
fgets(choice, 2, stdin);
/* This if statement is used to determine whether the user has entered more than the character limit and if so it gets rid of the excess data */
if(choice[strlen(choice)-1] != '\n')
{
eatExtraInput();
}
intChoice = atoi(&choice[0]);
/* This switch statement is used to call each function based on the user's input */
switch(intChoice)
{
case 1:
for(int i=0; i <= 100; i++){cout << endl;};
cout << "Please enter a new passenger ID: ";
cin >> passengerID;
if(passengers[passengerID].get_name() == "")
{
passengers[passengerID].createPassenger(passengerID);
}
else
{
cout << "*This passenger already exists\n\n";
cout << "Press the return key to continue...";
cin.ignore();
cin.get();
}
break;
case 2:
for(int i=0; i <= 100; i++){cout << endl;};
cout << "Please enter the ID of the passenger's details you wish to view: ";
cin >> passengerID;
if(passengers[passengerID].get_name() != "")
{
passengers[passengerID].displayDetails();
}
else
{
cout << "*This passenger does not exist\n\n";
cout << "Press the return key to continue...";
cin.ignore();
cin.get();
}
break;
case 3:
for(int i=0; i <= 100; i++){cout << endl;};
cout << "Please enter the ID of the passenger: ";
cin >> passengerID;
cout << "Please enter the ID of the flight: ";
cin >> flightID;
cout << "Please enter the unique seat ID e.g. '5D': ";
cin >> seatID;
cout << endl << "Flight ID: " << flightID << endl;
flights[atoi(flightID.c_str())].bookSeat(passengers[passengerID], seatID, atoi(flightID.c_str()));
break;
case 4:
for(int i=0; i <= 100; i++){cout << endl;};
break;
case 5:
for(int i=0; i <= 100; i++){cout << endl;};
cout << "Please enter the ID of the flight in which you would like to view: ";
cin >> flightID;
flights[atoi(flightID.c_str())].get_available_seats();
break;
case 6:
for(int i=0; i <= 100; i++){cout << endl;};
cout << "Press the return key to exit the program...";
cin.ignore();
cin.get();
exit(0);
break;
default :
for(int i=0; i <= 100; i++){cout << endl;};
printf("*Please enter a valid condition! \n");
break;
}
}while(intChoice != 1 || intChoice != 2 || intChoice != 3 );
}
/***********************************************************************************************************************************************/
/* */
/* main Function */
/* ------------- */
/* */
/* This function is used to call the mainMenu function */
/* */
/* */
/***********************************************************************************************************************************************/
int main(int argc, char *argv[])
{
/* This calls the mainMenu function and starts the programs main role */
mainMenu();
system("pause");
return 0;
}
passenger.h:
//
// passenger.h
// Scotia Airlines
//
// Created by Conner McCabe on 25/11/2012.
// Copyright (c) 2012 Conner McCabe. All rights reserved.
//
#ifndef __Scotia_Airlines__passenger__
#define __Scotia_Airlines__passenger__
#include <sstream>
#include <iostream>
using namespace std;
/***********************************************************************************************************************************************/
/* */
/* This function is used to swallow unwanted data stored in the buffer so that it is not passed onto the next line */
/* */
/***********************************************************************************************************************************************/
void eatExtraInput()
{
int c;
while ((c = getchar()) != '\n');
}
class passenger {
public:
int get_passenger_ID()
{
return passengerID;
}
string get_name()
{
return name;
}
int get_age()
{
return age;
}
string get_address()
{
return address;
}
float get_discount()
{
return discount;
}
int set_ID(int id)
{
if((passengerID = id))
{
return 1;
}
else
{
return 0;
}
}
int set_name(string passedName)
{
return 1;
}
int set_age(int passedAge)
{
if((age = passedAge))
{
return 1;
}
else
{
return 0;
}
}
int set_address(string passedAddress)
{
return 1;
}
int set_discount(int passedDiscount)
{
if((discount = passedDiscount))
{
return 1;
}
else
{
return 0;
}
}
void createPassenger(int id)
{
cin.ignore();
cin.clear();
passengerID = id;
cout << "Please enter the name of the Passenger: ";
getline(cin, name);
cout << "Please enter the Passenger's age: ";
cin >> age;
cin.ignore();
cin.clear();
cout << "Please enter the address of the Passenger: ";
getline(cin, address);
cout << "Please enter the Passenger's discount amount: ";
cin >> discount;
cout << "Press the return key to continue...";
cin.ignore();
cin.get();
}
void displayDetails()
{
cout << "ID: " << passengerID << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Address: " << address << endl;
cout << "Discount Amount: " << discount << "%" << endl;
cout << "Press the return key to continue...";
cin.ignore();
cin.get();
}
private:
string name;
int age;
int passengerID;
string address;
float discount;
};
#endif /* defined(__Scotia_Airlines__passenger__) */
flight.h:
//
// passenger.h
// Scotia Airlines
//
// Created by Conner McCabe on 25/11/2012.
// Copyright (c) 2012 Conner McCabe. All rights reserved.
//
#ifndef __Scotia_Airlines__passenger__
#define __Scotia_Airlines__passenger__
#include <sstream>
#include <iostream>
using namespace std;
/***********************************************************************************************************************************************/
/* */
/* This function is used to swallow unwanted data stored in the buffer so that it is not passed onto the next line */
/* */
/***********************************************************************************************************************************************/
void eatExtraInput()
{
int c;
while ((c = getchar()) != '\n');
}
class passenger {
public:
int get_passenger_ID()
{
return passengerID;
}
string get_name()
{
return name;
}
int get_age()
{
return age;
}
string get_address()
{
return address;
}
float get_discount()
{
return discount;
}
int set_ID(int id)
{
if((passengerID = id))
{
return 1;
}
else
{
return 0;
}
}
int set_name(string passedName)
{
return 1;
}
int set_age(int passedAge)
{
if((age = passedAge))
{
return 1;
}
else
{
return 0;
}
}
int set_address(string passedAddress)
{
return 1;
}
int set_discount(int passedDiscount)
{
if((discount = passedDiscount))
{
return 1;
}
else
{
return 0;
}
}
void createPassenger(int id)
{
cin.ignore();
cin.clear();
passengerID = id;
cout << "Please enter the name of the Passenger: ";
getline(cin, name);
cout << "Please enter the Passenger's age: ";
cin >> age;
cin.ignore();
cin.clear();
cout << "Please enter the address of the Passenger: ";
getline(cin, address);
cout << "Please enter the Passenger's discount amount: ";
cin >> discount;
cout << "Press the return key to continue...";
cin.ignore();
cin.get();
}
void displayDetails()
{
cout << "ID: " << passengerID << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Address: " << address << endl;
cout << "Discount Amount: " << discount << "%" << endl;
cout << "Press the return key to continue...";
cin.ignore();
cin.get();
}
private:
string name;
int age;
int passengerID;
string address;
float discount;
};
#endif /* defined(__Scotia_Airlines__passenger__) */
seat.h:
//
// seat.h
// Scotia Airlines
//
// Created by Conner McCabe on 26/11/2012.
// Copyright (c) 2012 Conner McCabe. All rights reserved.
//
#ifndef Scotia_Airlines_seat_h
#define Scotia_Airlines_seat_h
#include <iostream>
#include "passenger.h"
using namespace std;
class seat : public passenger {
public:
seat(){seatStatus = 0;}
int get_status()
{
return seatStatus;
}
void setStatus(int passedStatus)
{
seatStatus = passedStatus;
}
private:
int seatStatus;
};
#endif
Also, unrelated, but if possible, if anyone has experience with vectors I'd love to know how to use them, as they seem awfully complex when I last looked at them anyway, and I'm not really sure how they work.
Thanks as always guys, Conner.
This post has been edited by mccabec123: 05 December 2012 - 03:29 PM

New Topic/Question
Reply




MultiQuote





|