ive tried to make a bank account using inheritance and while writing the main file i am coming across an error. I am trying to pass a value to the balance and when i input a value to add it either outputs the wrong number or gives me -nan(id). I'll post my code below so maybe someone can see if i have any of the coding wrong. I am not the best at coding and anything will seriously help. The program is not entirely finished but i want to be able to understand why it is outputting the wrong numbers
A couple of suggestions concerning forum etiquette. If you put your code inside of "code tags", it will make it much easier to read. Code tags will display line numbers, and will also preserve your code's indentation. Using code tags makes it much more likely that others will want to help you. Here is a short article on how to use code tags on this forum:
Additionally, you've already made a thread about this bank account assignment. Instead of creating new threads for each question, simply reply to your own, initial thread. Anytime you or someone else posts in your thread it will get bumped to the top of the board.
Now, your main function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main()
{
double bal = 0;
double value = 0;
account set(bal);
account acc(bal);
account cred(value);
set.setBalance(bal);
cout << "Enter Initial Balance \n";
cin >> bal;
cout << "You're balance is " << acc.getBalance() << endl;
cout << "Enter amount to deposit into account \n";
cin >> value;
cout << "You're Balance is now \n" << cred.credit(value) << endl;
}
On lines 5 - 7, you're creating three individual, distinct accounts. One is named 'set', the others 'acc' and 'cred'. These names do not refer to the same account, these are three different accounts, each with their own data members and member functions. I think what you were trying to do is create a single account.
Line 8 is unnecessary.
On lines 10 - 12, you enter a value, and then immediately print the balance of the 'acc' account. The balance of that account is zero, so it will print zero.
Lines 3&4 assign values to two local variables. The local variables disappear as soon as the constructor exits. You want to set the member variables in the savingaccount object.
The balance gets set in already because you're initializing the account with account(bal) at line 1. You can initialize intrate by just removing double from line 4. Better yet, initialize it in the initialization list: