Jul 9, 2012 at 4:53pm UTC
I am getting the error 'Account::credit' : must return a value... any help would be greatly appreciated.
#include <iostream>
#include <string>
using namespace std;
class Account {
private:
float balance;
public :
Account (float cbalance)
{
if (cbalance > 0)
{
balance = cbalance;
}
else
{
balance = 0;
}
}
float getBalance ()
{
return balance;
}
float credit ( float amount )
{
balance += amount;
}
float debit ( float amount )
{
if ((balance - amount) < 0.0 )
{
cout << "Debit amount exceeded account balance" << endl;
return 0.0;
}
else
{
balance -= amount;
return amount;
}
}
};
int main () {
cout << "creating account 1 with $10 " << endl;
Account Acc1 = Account (10);
cout << "creating account 2 with $999 " << endl;
Account Acc2 = Account (999);
cout << "Account 1 Balance is : " << Acc1.getBalance() << endl;
cout << "Account 2 Balance is : " << Acc2.getBalance() << endl;
cout << "adding 100 to Account 1" << endl;
Acc1.credit(100);
cout << "Account 1 Balance is : " << Acc1.getBalance() << endl;
cout << "adding 1 to Account 2" << endl;
Acc2.credit(1);
cout << "Account 2 Balance is : " << Acc2.getBalance() << endl;
cout << "Account 1 - withdrawal for 200 : " << Acc1.debit(200) << endl;
cout << "Account 2 - withdrawal for 200 : " << Acc2.debit(200) << endl;
cout << "Account 1 - withdrawal for 100 : " << Acc1.debit(100) << endl;
cout << "Account 2 - withdrawal for 100 : " << Acc2.debit(100) << endl;
cout << "Account 1 Balance is : " << Acc1.getBalance() << endl;
cout << "Account 2 Balance is : " << Acc2.getBalance() << endl;
system("pause");
return 0;
}
Jul 9, 2012 at 4:57pm UTC
I think you forgot to put 'return' in your Credit function. It says it is returning a float, but in the function it only does the caculation, it isn't returning the answer.
return (balance += amount;)
Maybe that would work? Not sure about my code up there, but I am sure that you need to add the return statement. :)
Jul 9, 2012 at 5:07pm UTC
Yay!! That was my very first helpful answer, whoo hooo! :)