adding elements into vector

having a problem adding elements into a vector. it stalls the bracket and i think it is because of an out of bounds error, can anyone help? :/ occurs when i add a passenger in the main.


#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;

class Person
{
public:
Person();
double price;
void deletePassenger();
void addPassenger();
void printPassenger();
double costOfFlight(string location1, string class1);
void printPassengers();
private:
string forename;
string surname;
string passportNum;
string contactNum;
string email;
string location;
string flightNumber;
string passengerClass;

};

Person::Person()
{}

double Person::costOfFlight(string location1, string class1)
{
price = 0;
if (class1.compare("E") == 0 || class1.compare("e") == 0)
price = 200;
if (location1.compare("Europe") == 0)
{
return price = price + 100 + (rand() % 100);
}
else if (location1.compare("North_Africa") == 0)
{
return price = price + 250 + (rand() % 150);
}
else if (location1.compare("South_Africa") == 0)
{
return price = price + 350 + (rand() % 200);
}
else if (location1.compare("Asia") == 0)
{
return price = price + 600 + (rand() % 500);
}
else if (location1.compare("North_America") == 0)
{
return price = price + 700 + (rand() % 500);
}
else if (location1.compare("South_America") == 0)
{
return price = price + 700 + (rand() % 550);
}
else
{
return price = price + 500 + (rand() % 400);
}
}


void Person::addPassenger()
{
cout << "Enter Passenger Name: ";
cin >> forename;
cin >> surname;
cout << endl;
cout << "Passenger phone number: ";
cin >> contactNum;
cout << endl;
cout << "E-mail address: ";
cin >> email;
cout << endl;
cout << "Enter Passport Number: ";
cin >> passportNum;
cout << endl;
cout << "Enter destination: ";
cin >> location;
cout << endl;
cout << "Passenger flight number: ";
cin >> flightNumber;
cout << endl;
cout << "Passenger class: ";
cin >> passengerClass;
cout << endl;
cout << "Total price (including VAT): ";
cout << costOfFlight(location, passengerClass) << endl;
}

void Person::deletePassenger()
{
delete &forename;
delete &surname;
delete &passportNum;
delete &email;
delete &contactNum;
delete &location;
delete &flightNumber;
delete &passengerClass;
delete &price;
}

void Person::printPassengers()
{
cout << "Name: \t\t\t\t" << surname << ", " << forename << endl;
cout << "Passenger phone number: \t" << contactNum << endl;
cout << "E-mail address: \t\t" << email << endl;
cout << "Enter Passport Number: \t\t" << passportNum << endl;
cout << "Enter destination: \t\t" << location << endl;
cout << "Passenger flight number: \t" << flightNumber << endl;
cout << "Passenger class: \t\t" << passengerClass << endl;
cout << "Total price (including VAT): \t" << price << endl;
}
vector<Person>::iterator it;
int main()
{
string repeat;
string answer;
string name;
int i = 0;
int j;
vector<Person> passengerDatabase;

do
{
cout << "Enter the number to execute the following: \n"
<< "- Press 0 to add a Passenger\n"
<< "- Press 1 to see all Passengers\n"
<< "- Press 2 to delete most recent Passenger\n"
<< "- Press 3 to exit program\n\n"
<< "Number: ";
cin >> j;
switch (j)
{
case 0:
{
system("cls");
Person passenger;
passenger.addPassenger();
passengerDatabase[i] = passenger;
i++;
cout << i;
break;
}
case 1:
system("cls");
for ( it = passengerDatabase.begin(); it < passengerDatabase.end(); it++ )
it->printPassengers();
break;
case 2:
cout << "Are you sure you want to delete the last person added? (Y/n)";
cin >> answer;
if (answer.compare("N") != 0 || answer.compare("n") != 0)
{
i--;
cout <<"\nThe most recent person added has been deleted\n";
}
cout << endl;
break;
case 3:
exit(1);
break;
case 4:
system("cls");
cout << "Please Enter the passenger you wish to cancel: ";
cin >> name;
cout << endl;
//cancelPassenger(name, passengerDatabase);
break;
default:
cout << "Please enter an appropriate value!";
}
cout << "Would you like to repeat previous options? (Y/n)";
cin >> repeat;
if (repeat.compare("n") != 1 || repeat.compare("N") != 1)
{
exit(1);
}
cout << endl;
system("cls");
}

while(repeat.compare("n") != 0 || repeat.compare("N") != 0);
return 0;
}
also if i change the line

passengerDatabase[i] = passenger;

to

passengerDatabase.at(i) = passenger;

it gives me a real weird error that i have to call the manufacturer O_O
The [] operator does not throw exceptions for out of bounds, etc, whereas the at() function does throw such exceptions. SInce you don't catch the exceptions, your program terminates in an 'unusual way' and the operating system you use will generally fire up some message like that because something went wrong.

When you make a vector with the default constructor like that, it has a size of 0. So no matter what index you use it will always be out of bounds. I think that what you want to do is add new elements to it, which would be using the push_back() function. The .at() and operator[] functions are for existing elements.
Last edited on
yeah the [] operator jus kills the programme and asks if i want to debug the error, the thing is i need the vector to change size. what would i have to use to access the vector to add a new element?
LB wrote:
what you want to do is add new elements to it, which would be using the push_back() function.
http://www.cplusplus.com/reference/stl/vector/push_back/
Last edited on
ok that fixed it, previously i had Person passenger; created globally and that made the code reeeeeally rediculously flawed but a friend of mine pointed that out and this fix made it even more reliable lol thank you so much :)
This is a little bit off topic, but in void Person::deletePassenger()... did you by any chance mean to use clear()?
http://cplusplus.com/reference/string/string/clear/

-Albatross
Yeah...you shouldn't be deleting memory that you didn't make with new in the first place. The strings will be deleted automatically with your class, you don't need code for that at all.
its ok i figured all that out the programme is much neater and isnt so weird :S if anyone is interested here is the code :) now bare with me its not gonna compile as im still working on it and all but its much better now :):



//#include "Flight.cpp"
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;

class Person
{
public:
Person();
double price;
void addPassenger();
void printPassenger();
double costOfFlight(string location1, string class1);
void printPassengers();
string forename;
string surname;
string passportNum;
string email;
private:
string contactNum;

string location;
string flightNumber;
string passengerClass;

};

Person::Person()
{}

double Person::costOfFlight(string location1, string class1)
{
price = 0;
if (class1.compare("E") == 0 || class1.compare("e") == 0)
price = 200;
if (location1.compare("Europe") == 0)
{
return price = price + 100 + (rand() % 100);
}
else if (location1.compare("North_Africa") == 0)
{
return price = price + 250 + (rand() % 150);
}
else if (location1.compare("South_Africa") == 0)
{
return price = price + 350 + (rand() % 200);
}
else if (location1.compare("Asia") == 0)
{
return price = price + 600 + (rand() % 500);
}
else if (location1.compare("North_America") == 0)
{
return price = price + 700 + (rand() % 500);
}
else if (location1.compare("South_America") == 0)
{
return price = price + 700 + (rand() % 550);
}
else
{
return price = price + 500 + (rand() % 400);
}
}


void Person::addPassenger()
{
cout << "Enter Passenger Name: ";
cin >> forename;
cin >> surname;
cout << endl;
cout << "Passenger phone number: ";
cin >> contactNum;
cout << endl;
cout << "E-mail address: ";
cin >> email;
cout << endl;
cout << "Enter Passport Number: ";
cin >> passportNum;
cout << endl;
cout << "Enter destination: ";
cin >> location;
cout << endl;
cout << "Passenger flight number: ";
cin >> flightNumber;
cout << endl;
cout << "Passenger class: ";
cin >> passengerClass;
cout << endl;
cout << "Total price (including VAT): ";
cout << costOfFlight(location, passengerClass) << endl;
}

void Person::printPassengers()
{
cout << "Name: \t\t\t\t" << surname << ", " << forename << endl;
cout << "Passenger phone number: \t" << contactNum << endl;
cout << "E-mail address: \t\t" << email << endl;
cout << "Enter Passport Number: \t\t" << passportNum << endl;
cout << "Enter destination: \t\t" << location << endl;
cout << "Passenger flight number: \t" << flightNumber << endl;
cout << "Passenger class: \t\t" << passengerClass << endl;
cout << "Total price (including VAT): \t" << price << endl;
}

vector<Person>::iterator it;

int main()
//int PassengerMain()
{
string repeat;
string answer;
string name;
int i = 0;
int j;
vector<Person> passengerDatabase;

do
{
cout << "Enter the number to execute the following: \n"
<< "- Press 0 to add a Passenger\n"
<< "- Press 1 to see all Passengers\n"
<< "- Press 2 to delete most recent Passenger\n"
<< "- Press 3 to exit program\n\n"
<< "Number: ";
cin >> j;
switch (j)
{
case 0:
{
system("cls");
Person passenger;
passenger.addPassenger();
passengerDatabase.push_back(passenger);
it++;
break;
}
case 1:
system("cls");
for ( it = passengerDatabase.begin(); it < passengerDatabase.end(); it++ )
it->printPassengers();
break;
case 2:
cout << "Are you sure you want to delete a passenger from the database? (Y/n)";
cin >> answer;
if (answer.compare("N") != 0 || answer.compare("n") != 0)
{
string forename, surname, passportNum;
cout << "Enter a forename, surname and passport number: \n";
cin >> forename;
cin >> surname;
cin >> passportNum;
for (it = passengerDatabase.begin(); it < passengerDatabase.end(); ++it)
{
if ((passengerDatabase.at(it).forename).compare(forename) == 0 && (passengerDatabase.at(it).surname).compare(surname) == 0 && (passengerDatabase.at(it).passportNum).compare(passportNum) == 0)
{
cout << passengerDatabase.at(it).forename << endl;
cout << passengerDatabase.at(it).surname << endl;
cout << passengerDatabase.at(it).email << endl;
cout << passengerDatabase.at(it).price << endl;
passengerDatabase.erase(it);
it--;
cout <<"\nThe most recent person added has been deleted\n";
}
else
{
cout << "The person does not exist on the database.\n";
}
}
}
cout << endl;
break;
case 3:
exit(1);
break;
case 4:
system("cls");
cout << "Please Enter the passenger you wish to cancel: ";
cin >> name;
cout << endl;
//cancelPassenger(name, passengerDatabase);
break;
default:
cout << "Please enter an appropriate value!";
}
cout << "Would you like to repeat previous options? (Y/n)";
cin >> repeat;
if (repeat.compare("n") != 1 || repeat.compare("N") != 1)
{
exit(1);
}
cout << endl;
system("cls");
}

while(repeat.compare("n") != 0 || repeat.compare("N") != 0);
return 0;
}





/*for (int searchData = 0; searchData < passengerDatabase.size(); searchData++)
{
if ((passengerDatabase.at(searchData).forename).compare(forename) == 0 && (passengerDatabase.at(searchData).surname).compare(surname) == 0 && (passengerDatabase.at(searchData).passportNum).compare(passportNum) == 0)
{
cout << passengerDatabase.at(searchData).forename << endl;
cout << passengerDatabase.at(searchData).surname << endl;
cout << passengerDatabase.at(searchData).email << endl;
cout << passengerDatabase.at(searchData).price << endl;
passengerDatabase.erase(it);
it--;
cout <<"\nThe most recent person added has been deleted\n";
}
else
{
cout << "The person does not exist on the database.\n";
}
}*/
hmmm having issue with deleting an element :/ i have gotten rid of the delete passenger and im using the .erase(); instead, but i have to work with an iterator rite? but when i try to increment the iterator it doesnt, it stays in place. here is the delete part:

string forename1, surname1, passportNum1;
cout << "Enter a forename, surname and passport number: \n";
cin >> forename1;
cin >> surname1;
cin >> passportNum1;
for (int searchData = 0; searchData < passengerDatabase.size(); searchData++)
{
for (it = passengerDatabase.begin(); it != passengerDatabase.end(); ++it)
{
if ((passengerDatabase.at(searchData).forename).compare(forename1) == 0 && (passengerDatabase.at(searchData).surname).compare(surname1) == 0 && (passengerDatabase.at(searchData).passportNum).compare(passportNum1) == 0)
{
cout << searchData;
passengerDatabase.erase(it);
cout <<"\nThe most recent person added has been deleted\n";
break;
}
else
{
cout << "The person does not exist on the database.\n";
}
}
}
Topic archived. No new replies allowed.