Hi guys
I'm stll playing around with classes. I have some simple class, the program itself works but when I enter the value for your_account as a negative number it doesn't pass the right numbers to the private variables. Why is that? How may I fixed it
Thanks
#include <iostream>
#include <conio.h>
using namespace std;
class Money
{
public:
Money (long dollars,int cents);
Money (long dollars);
Money ( );
void input (istream& ins);
void output (ostream& out);
private:
long dollar_amount;
int cent_amount;
void check (long dollars,int cents);
void Money::check(long dollars);
double Money::get_balance ( );
};
int main ( )
{
Money your_account,my_account(10,9),our_account;
cout<<"\nYour account is equall to: ";
your_account.output(cout);
cout<<"\nMy account is equall to: ";
my_account.output(cout );
your_account.input (cin);
your_account.output(cout);
getch();
return 0;
}
Money::Money (long dollars,int cents)
{
dollar_amount=dollars;
cent_amount=cents;
check(dollar_amount,cent_amount);
}
Money::Money (long dollars)
{
dollar_amount=dollars;
cent_amount=0;
check (dollar_amount);
}
void Money::check(long dollars,int cents)
{
if (dollars<0||cents<0)
{
cout<<"Illigal values";
getch();
exit (1);
}
}
Money::Money ( )
{
dollar_amount=0;
cent_amount=0;
}
void Money::check(long dollars)
{
if (dollars<0)
{
cout<<"Illigal values";
getch();
exit (1);
}
}
void Money::output (ostream& out)
{
out.setf(ios::fixed);
out.setf(ios::showpoint);
out.precision(2);
out<<get_balance( );
}
double Money::get_balance ( )
{
return (dollar_amount+(cent_amount*0.01));
}
void Money::input (istream& ins)
{
cout<<"\n\nYou are going to set up the amount for your_account"<<endl;
cout<<"Enter the amount to your_account: ";
bool negative;
char first_character;
char decimal_point;
if (first_character=='-')
{
negative=true;
ins>>first_character;
}
else negative=false;
if(negative)
{
ins>>dollar_amount>>decimal_point>>cent_amount;
dollar_amount=-dollar_amount;
cent_amount=cent_amount;
}
else
ins>>dollar_amount>>decimal_point>>cent_amount;
}