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

Dani AI

Generated

The program stopped because of a loop-condition typo that let the loop run past the vector bounds. found and fixed it — the symptom ("quits working" right after erase) is classic: an infinite loop or an out-of-range index while indexing the vector. ’s reminder about reading and careful question-asking is also useful background for avoiding these simple mistakes.

Common causes and safer habits:

  • A loop condition that accidentally uses a container expression (or its size) as a boolean will evaluate true while the container is non-empty, so the index keeps increasing and eventually goes out of range. Always compare the index to the size.
  • Use the correct index type: prefer size_t or auto for i to avoid signed/unsigned surprises when comparing to vector::size().
  • Prefer range-based loops (C++11+) to avoid manual indexing entirely, or use iterators when modifying the container during traversal.
  • When removing elements, prefer a find-then-erase or the erase-remove idiom rather than manually computing offsets that can become invalid after mutations.

Practical patterns (generic examples):

for (size_t i = 0; i < vec.size(); ++i)
    vec[i].do_something();

for (const auto& e : vec)
    e.do_something();

vec.erase(std::remove_if(vec.begin(), vec.end(),
    [&](const T& e){ return matches_target(e); }), vec.end());

Other troubleshooting tips:

  • Compile with warnings enabled (e.g. -Wall -Wextra) and run with sanitizers (AddressSanitizer / UndefinedBehaviorSanitizer) or valgrind to catch out-of-bounds or undefined behavior.
  • Read the container docs for iterator invalidation rules (vector::erase behavior) and the erase-remove idiom: std::vector::erase and .

Recommended Answers

All 3 Replies

Problem solved...typo in for loop

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

Sorry I am just trying to see different answers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.