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
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";
choice = stoi(tempChoice);
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
cout << "Please input the number of the account you want to view\n";
cin >> n;
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
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
fubar = 1234; //this may work. it often does. but you never allocated  with a push-back or other allocation.... so its not right, but even so, it tends to work.
cout << fubar;
//program ends, crash: it tried to delete memory it didn't allocate.
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
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 , (n = 0), the next is  … and so on. if you typed in a couple and did  and  type things is probably working. if you typed like 1234, not so much. ?