keep getting the output -nan(id) when i try to add to my balance

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




#include <iostream>
using namespace std;

//account header file

#ifndef account_h
#define account_h

class account
{

public:
account(double bal);
double credit(double value);
bool debit(double value);
double getBalance();
void setBalance(double bal);

private:
double balance;




};

#endif


//account.cpp



#include "account.h"
#include <iostream>
using namespace std;


account::account(double bal)
{
if (bal<0)
{
cout << "\n Invalid Balance. Balance will be set to 0 \n";
balance = 0;
}
else
{
balance = bal;
}
}

double account::credit(double value)
{
if (value > 0)
{
balance=(balance + value);
}
else
{

cout << "\n Invalid Value \n" << value;
return balance;
}

}
bool account::debit(double value)
{
bool enoughfunds;
{

if (value > balance)
{
enoughfunds = false;
cout << "Insufficient Funds" << balance;

return false;


}
else
{
enoughfunds = true;
balance -= value;
cout << "Transaction completed succcessfully." << endl;
cout << "You're current balance is" <<balance<<endl;

return true;

}
}
}
double account::getBalance()
{
return balance;
}

void account::setBalance(double bal) {
balance = bal;

}




// savingaccout.h




#include <iostream>
#include"account.h"
using namespace std;


#ifndef savingaccount_h
#define savingaccount_h


class savingaccount : private account
{
public:
savingaccount(double bal, double percentage);
double calculateIntrate(double percentage, double bal);
double getIntrate();
void setIntrate(double percentage);

private:
double intrate;
};

#endif





//savingaccout.cpp



#include <iostream>
#include "savingaccount.h"


using namespace std;

savingaccount::savingaccount(double bal, double percentage) :account(bal)
{
double balance = bal;
double intrate = percentage;
}
double savingaccount::calculateIntrate(double percentage, double bal)
{
return bal*(percentage / 100);

}

double savingaccount::getIntrate() {
return intrate;
}
void savingaccount::setIntrate(double percentage) {
intrate = percentage;
}



//checkingaccount.h




#include <iostream>
#include "account.h"
using namespace std;


#ifndef checkingaccount_h
#define checkingaccount_h


class checkingaccount : private account

{
public:
checkingaccount(double bal, double Cfee);
double Chargefee(double bal, double Cfee);
double getfee();
void setfee(double Cfee);


private:
double fee;
};
#endif



//checkingaccout.cpp


#include <iostream>
#include "checkingaccount.h"
using namespace std;


checkingaccount::checkingaccount(double bal, double Cfee) :account(bal)
{
double balance = bal;
double fee = Cfee;

}
double checkingaccount::Chargefee(double bal, double Cfee)
{
if ((bal - fee) >= 0)
{
bal -= fee;
cout << "Fee charged succcessfully.\n";
return true;
}
else {
return false;

}
}
double checkingaccount::getfee()
{
return fee;
}
void checkingaccount::setfee(double Cfee)
{
fee = Cfee;

}



//main


#include <iostream>
#include "account.h"
#include "savingaccount.h"
#include "checkingaccount.h"
using namespace std;

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;
Last edited on
shamywamyx,

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:

http://www.cplusplus.com/articles/jEywvCM9/

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.

Line 15 fails for a similar reason.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main() {

	double input = 0.0;

	account the_account(0.0);

	std::cout << "Enter initial balance: ";
	std::cin >> input;

	the_account.setBalance(input);

	std::cout << "Your balance is " << the_account.getBalance() << std::endl;

	std::cout << "Enter the amount to deposit: ";
	std::cin >> input;

	std::cout << "Your new balance is " << the_account.credit(input) << std::endl;

	return 0;
}
1
2
3
4
5
savingaccount::savingaccount(double bal, double percentage) :account(bal)
{
double balance = bal;
double intrate = percentage;
}

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:
1
2
3
4
savingaccount::savingaccount(double bal, double percentage) :
    account(bal),
    intrate(percentage)
{}
Topic archived. No new replies allowed.