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;
      
}

Recommended Answers

All 6 Replies

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;

cin is a global istream object that is predeclared for you. You don't
need to pass an istream object reference to this function. Just use
cin within the function as desired.

You don't give first_character a value before you try to compare it to '-'
using the equals operator. This is wrong. first_char will have an
unknown value under these circumstances, and it will probably never
be '-' based on chance.

Assuming user input is something like: 100.00

then this line

ins>>dollar_amount>>decimal_point>>cent_amount

is going to cause the istream object to either go into a fail state or
go into an infinite loop when it encounters the decimal point. Either way
the program is doomed to fail unless the problem is corrected. My advise
would be to change dollar_amount to type double and forget about all the
hassle you are creating for yourself, unless this is an assignment and the
current program structure is required.

this is not an assignment. I'm just playing around ,trying to do many things on classes. I just trying to find a way to do excutly as I did but in a correct way.
thanks anyway

Hi again. I think I found a good way to approach the problem. I think :) . Look at the code:

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;
  int first_number,second_number,all_cents;
  
     if (first_character=='-')
    negative=true;
    else negative=false;
    if(negative)
    first_number=-first_number;
    ins>>first_number>>decimal_point>>second_number;
    cout<<first_number<<"."<<second_number;
  
}

What that function return is
the number I entered : -34.56
th eoutput was : -34.560.00

Now I have a new question. How may I delete the last for characters including the dot.
thanks

Never mind.
What a stupid ass I am :). I got it.
Thanks

Actually one more question
I did that so far:

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;
  int first_number,second_number,all_cents;
  ins>>first_character;
    
    if (first_character=='-')
    negative=true;
    else negative=false;
    if(negative)
    {
    ins>>first_number>>decimal_point>>second_number;
    //cout<<first_number<<"."<<second_number;
    dollar_amount=-first_number;
    cent_amount=-second_number;
    }
    if(isdigit(first_character))
    {
    ins>>dollar_amount>>decimal_point>>cent_amount;
    //second_number=static_cast<int>(first_character);
    
    }
}

what i'm loosing right now is just the first digit of the positive number. How may I add that lost number which is a character to the dollar_amount so it will hold an whole number.
thanks

>>what i'm loosing right now is just the first digit of the positive number. How may I add that lost number which is a character to the dollar_amount so it will hold an whole number

My first advice is to redo the class by changing the member variable types so you don't have to go through all this other hassle.

If, for whatever reason you wish to persist in your current approach, then I would convert the char to a string, convert the integer to a string, append the integer string on to the char string and then convert the concatenated string back to an integer type.

To do this I first suggest looking into use of string streams.

However, since you're using conio.h anyway, I believe you could use itoa() (which I believe is in most versions of conio.h) to convert the integer value to a string, and strtol() or atoi() (or similar function) to turn the concatenated string back to integer value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.