Debug Error! abort() has been called

Hi everyone, I was making a kind of bank application just to get some practice in, and I've come upon an error i haven't been able to solve by googling or experimenting.
Basically when I select the menu option to print the balance of the account the program crashes and I get the error in the title, where it states "abort() has been called"
Any help would be appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

int main()
{
	string tempChoice;
	bank b;
	int choice;
	do
	{
		cout << "Welcome to " << b.bankName << "\nPlease make your choice\n";
		cout << "1. Open new account\n2. Check balance\n3. Deposit\n4. Withdraw\n5. View all accounts\n0. Exit\n";
		getline(cin, tempChoice);
		choice = stoi(tempChoice);
		switch (choice)
		{
		case 1:
			b.createAccount();
			break;
		case 2:
			b.displayBalance();
			break;
		case 3:
			b.deposit();
			break;
		case 4:
			b.withdraw();
			break;
		case 5:
			b.printAccountNums();
			break;
		default:
			cout << "No valid option selected.\n";
		}


	} while (choice != 0);
}

	void printbal() // the printbal function from the bankAccount class
	{
		cout << "The balance of " << holderName << "'s" "account is " << balance << endl;
	}

        void displayBalance() // displaybalance function in bank class
	{
		int n;
		cout << "Please input the number of the account you want to view\n";
		cin >> n;
		accounts[n].printbal();
	}
does the word 'assert' appear anywhere? And if it does, can you provide the text associated with that word?

what I see, which normally causes trouble but not crashes, is that you mixed cin and getline, which can cause unexpected behavior.

second thing is 'n' vs accounts. what is accounts? If n is -13, what should happen?
if accounts is an object with its own save [] it may be fine, if its an array or vector, it may implode.
Last edited on
I have not used assert anywhere, to double check I searched the entire solution and it doesn't show up.

I did have trouble with mixing cin and getline, which is why in main() i take choice as a string and convert it with stoi, as otherwise the program would not wait for the input for the name when creating a new account and just skip to the next cin

Accounts is a vector of type bankAccount, and each time a new account is created it's pushed back, so accounts and n is just a way of keeping track of the accounts. I'm not sure if there's a better way to do it, I thought about it for a while and couldn't think of another way to do it so that's what I came up with. What's weird is something i just noticed now is that the balance of accounts[n] does get successfully printed, but after that the program crashes
ok. it sounds to me like N may be your problem.
put this around it:
cin >> n;
if (n >=0 && n < accounts.size())
accounts[n].printbal();
else
cout << "oops\n";

see if it crashes or prints oops doing that. if it prints oops, n was bad. If it crashes, its some other problem.

FYI there are asserts in some of the default libraries (eg cout and so on) in debug mode.
you can also try seeing where it blows out in your debugger.

I highly suspect you are gonna see that oops.
Last edited on
Hi jonnin, thanks again for your help

I added the if statement you suggested and unfortunately it made no difference to the code, the balance still gets printed and the program crashes.

I thought to use an STL array just to see if it would work but for some reason it threw up a ton of errors about referencing deleted functions that i was not able to work through.

However i think you're right about n being the problem since i tested the deposit and withdraw functions that use a similar method with cin and n to access the accounts for the first time and they exhibit the same behavior of working correctly but throwing up the error after they're done
Last edited on
right.
look at this snippet:

vector<int> fubar;
fubar[1] = 1234; //this may work. it often does. but you never allocated [1] with a push-back or other allocation.... so its not right, but even so, it tends to work.
cout << fubar[1];
//program ends, crash: it tried to delete memory it didn't allocate.

this is very likely what you are seeing.
I'm not sure I'm following, I thought the space in the vector would get allocated by the createaccount() function since it pushes back the accounts? I'm not sure how I'd do it otherwise

edit: solved the error, it turned out to be an exception being thrown by stoi, i randomly saw something about stoi causing exceptions and put it in a try/except statement, now the program doesn't crash, but i do see the oops now
Last edited on
ok, so if you see the oops, n is out of bounds.
I dunno without you telling me what you typed in to run the program, though. I mean if you type in a legal n, it will work fine, and if you don't, it won't...

an empty vector, the first push-back is [0], (n = 0), the next is [1] … and so on. if you typed in a couple and did [0] and [1] type things is probably working. if you typed like 1234, not so much. ?
Last edited on
I added an if statement to catch if n is out of bounds and it works as expected, all the code works now. Thanks a lot for your help
Topic archived. No new replies allowed.