# Vector Delete Problem

Page 1 of 1

## 2 Replies - 675 Views - Last Post: 18 April 2010 - 12:04 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=168691&amp;s=938dfbbbe70cc86b38f821c22ce79551&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tarheelfan_08

• D.I.C Regular

Reputation: -2
• Posts: 256
• Joined: 24-January 10

# Vector Delete Problem

Posted 17 April 2010 - 11:39 PM

Hey guys! I have my code wrote and when I run it, the code gets all the way to this point
```//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!

```#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";
}

```

```#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;
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";

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;
}

```

Is This A Good Question/Topic? 0

## Replies To: Vector Delete Problem

### #2 sarmanu

• D.I.C Lover

Reputation: 966
• Posts: 2,362
• Joined: 04-December 09

## Re: Vector Delete Problem

Posted 17 April 2010 - 11:49 PM

In fact, that "erase" is OK. The problem is the typo in this for loop:
```//Print Details of the Vector
for (int i = 0; AccountVector.size(); i++)

```

This post has been edited by sarmanu: 17 April 2010 - 11:50 PM

### #3 tarheelfan_08

• D.I.C Regular

Reputation: -2
• Posts: 256
• Joined: 24-January 10

## Re: Vector Delete Problem

Posted 18 April 2010 - 12:04 AM

Dang how did I miss that...thanks man!