Thread: Vector Delete Problem

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    20

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

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    20
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM