# Vector Delete Problem

• 04-18-2010
tarheelfan_08
Vector Delete Problem
Hey guys! I have my code wrote and when I run it, the code gets all the way to this point
Code:

```//Delete Account 333411         cout << "Here you will delete the account 333411: \n\n";         AccountVector.erase (AccountVector.begin()+2);                         //Print the details of the vector     for (int i = 0; i < AccountVector.size(); i++)     AccountVector[i].account_details();         cout << "----------------------------------------------------------------------\n";```
And it quits working! Can someone please tell me why!

Code:

```#include <iostream> #include <fstream> #include <iomanip> #include <functional> #include <algorithm> #include <string> #include <cstdlib> #include <sstream> using namespace std; //Account class class Account { private:         int number;         double balance;         double rate; public:         Account(); // Constructor         Account (int, double, double);         void setNumber(int);     void setBalance(double);     void setRate(double);         int getNumber();     double getBalance();     double getRate();         void account_details();         string string_account_details(); }; Account::Account() {         number =0;         balance = 0;         rate = 0; } Account::Account(int number, double balance, double rate) {                 Account::number = number;                 Account::balance = balance;                                 Account::rate= rate; } void Account::setNumber(int number) {     Account::number = number; } void Account::setBalance(double balance) {     Account::balance = balance; } void Account::setRate(double rate) {     Account::rate = rate; } int Account::getNumber() {     return number; } double Account::getBalance() {     return balance; } double Account::getRate() {     return rate; } void Account::account_details() {         cout << "The current account number is " << number <<                 " with a balance of \$" << balance << " and an interest rate of " << rate << ".\n\n"; }```
Code:

```#include "Account.h" #include <vector>  // this allows user to create a vector using namespace std; int main() {         const int SIZE = 6;         //Account array         Account Account_array[SIZE] = { Account(126455, 1000.54, .03),                                                                         Account(254774, 1500.44, .03),                                                                         Account(333411, 230.00, .02),                                                                         Account(491377, 1950.50, .03),                                                                         Account(677134, 5164.11, .04),                                                                         Account(765473, 2540.10, .04)};         // Declare a vector capable of storing classes         std::vector<Account> AccountVector;         //Assign individual accounts to vector         for (int i = 0; i < SIZE; i++)                 AccountVector.push_back(Account_array[i]);         for (int i = 0; i < SIZE; i++)         {                 std::cout << AccountVector[i].getNumber() << "\n";                 std::cout << AccountVector[i].getBalance() << "\n";                 std::cout << AccountVector[i].getRate() << "\n\n";         }         cout << "----------------------------------------------------------------------\n";         for (int i = 0; i < SIZE; i++)                 AccountVector[i].account_details();         cout << "----------------------------------------------------------------------\n";         //Add Accounts to Inventory         cout << "Here you will add 2 new accounts to the Vector: \n\n";         AccountVector.push_back( Account(123456, 5000.24, .01));         AccountVector.push_back( Account(489076, 121.50, .04));         AccountVector.push_back( Account(743216, 2500.10, .03));         //Print Details of the Vector         for (int i = 0; AccountVector.size(); i++)         AccountVector[i].account_details();         cout << "----------------------------------------------------------------------\n";         //Delete Account 333411         cout << "Here you will delete the account 333411: \n\n";         AccountVector.erase (AccountVector.begin()+2);                         //Print the details of the vector     for (int i = 0; i < AccountVector.size(); i++)     AccountVector[i].account_details();         cout << "----------------------------------------------------------------------\n";                         return 0; }```
• 04-18-2010
tarheelfan_08
Problem solved...typo in for loop
Code:

```#include "Account.h" #include <vector>  // this allows user to create a vector using namespace std; int main() {         const int SIZE = 6;         //Account array         Account Account_array[SIZE] = { Account(126455, 1000.54, .03),                                                                         Account(254774, 1500.44, .03),                                                                         Account(333411, 230.00, .02),                                                                         Account(491377, 1950.50, .03),                                                                         Account(677134, 5164.11, .04),                                                                         Account(765473, 2540.10, .04)};         // Declare a vector capable of storing classes         std::vector<Account> AccountVector;         //Assign individual accounts to vector         for (int i = 0; i < SIZE; i++)                 AccountVector.push_back(Account_array[i]);         for (int i = 0; i < SIZE; i++)         {                 std::cout << AccountVector[i].getNumber() << "\n";                 std::cout << AccountVector[i].getBalance() << "\n";                 std::cout << AccountVector[i].getRate() << "\n\n";         }         cout << "----------------------------------------------------------------------\n";         for (int i = 0; i < SIZE; i++)                 AccountVector[i].account_details();         cout << "----------------------------------------------------------------------\n";         //Add Accounts to Inventory         cout << "Here you will add 2 new accounts to the Vector: \n\n";         AccountVector.push_back( Account(123456, 5000.24, .01));         AccountVector.push_back( Account(489076, 121.50, .04));         AccountVector.push_back( Account(743216, 2500.10, .03));         //Print Details of the Vector         for (int i = 0; i < AccountVector.size(); i++)         AccountVector[i].account_details();         cout << "----------------------------------------------------------------------\n";         //Delete Account 333411         cout << "Here you will delete the account 333411: \n\n";         AccountVector.erase (AccountVector.begin()+2);                         //Print the details of the vector     for (int i = 0; i < AccountVector.size(); i++)     AccountVector[i].account_details();         cout << "----------------------------------------------------------------------\n";                         return 0; }```