Output Error

I can't figure out why I'm getting Your balance is 1.6767e-307 when I hit b after the first prompt. Any ideas?

#include<iostream>


using namespace std;

class Account
{
public:
Account();
Account(double balance);
void add_balance();
void withdraw_balance();
double get_balance() const;
private:
double balance;
double add;
double withdraw;
};

Account::Account()
{
double balance = 0.0;
double add = 0.0;
double withdraw = 0.0;
}

void Account::add_balance()
{
cout << "How much would you like to deposit?" << endl;
cin >> add;
double plus_balance = balance + add;
cout << "Your balance is " << plus_balance << endl;
}

void Account::withdraw_balance()
{
cout << "How much would you like to withdraw?" << endl;
cin >> withdraw;
if (withdraw > balance)
{
cout << "Insufficient Funds" << endl;
balance = balance - 5;
}
else
{
double minus_balance = balance - withdraw;
cout << "Your balance is " << minus_balance << endl;
}
}

double Account::get_balance() const
{
cout << "Your balance is " << balance << endl;
}

int main ()
{
Account current;

char command = ' ';
cout << "Press 'D' for deposit, 'W' for withdrawal, 'B' for balance"
<< endl;
cin >> command;
if (toupper(command) == 'D')
{
command = toupper(command);
current.add_balance();
}
else if (toupper(command) == 'W')
{
command = toupper(command);
current.withdraw_balance();
}
else if (toupper(command) == 'B')
{
command = toupper(command);
current.get_balance();
}
else cout << "Invalid Response" << endl;


system("pause");
return 0;
}




Last edited on
I'm suprised that compiled. The following function is missing its return statement. Unless you need to return a double, you should change it to void:

1
2
3
4
double Account::get_balance() const
{
cout << "Your balance is " << balance << endl;
}


Also, take a look at your constructor:

1
2
3
4
5
6
Account::Account()
{
double balance = 0.0;
double add = 0.0;
double withdraw = 0.0;
}


In your class you have declared your private data members, which would be initialized in the constructor once you instantiate your object. But instead what's happening is you have declared and initialized local variables in your constructor that only exist in the scope of the constructor. So when you enter b and call your current.get_balance() function, its outputing the balance data member which was never initialized in the constructor. So it's just outputting garbage. Remove the doubles from your variables in the construtor. Hope this helps.

Return 0;
Return 0
I'm suprised that compiled. The following function is missing its return statement.


He's probably using mingw. I've noticed that mingw compiler by default won't flag that particular error.
Return 0,

Thanks. An overlooked error on my part about the double statement not returning anything. That was the last of four Class Implementations I had been working on for 6 hours. Guess I should have had a little more caffeine. Thanks for your help on that and the constructor issue.

guest,

I'm using bloodshed. Not sure how this slipped by either...
Topic archived. No new replies allowed.