User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,934 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,405 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 456 | Replies: 6
Reply
Join Date: Jan 2007
Posts: 65
Reputation: mauro21pl is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mauro21pl mauro21pl is offline Offline
Junior Poster in Training

class

  #1  
Aug 7th, 2007
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;
      
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 1,157
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 152
Lerner Lerner is offline Offline
Veteran Poster

Re: class

  #2  
Aug 7th, 2007
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.
Reply With Quote  
Join Date: Jan 2007
Posts: 65
Reputation: mauro21pl is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mauro21pl mauro21pl is offline Offline
Junior Poster in Training

Re: class

  #3  
Aug 7th, 2007
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
Reply With Quote  
Join Date: Jan 2007
Posts: 65
Reputation: mauro21pl is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mauro21pl mauro21pl is offline Offline
Junior Poster in Training

Re: class

  #4  
Aug 7th, 2007
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
Reply With Quote  
Join Date: Jan 2007
Posts: 65
Reputation: mauro21pl is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mauro21pl mauro21pl is offline Offline
Junior Poster in Training

Re: class

  #5  
Aug 7th, 2007
Never mind.
What a stupid ass I am . I got it.
Thanks
Reply With Quote  
Join Date: Jan 2007
Posts: 65
Reputation: mauro21pl is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
mauro21pl mauro21pl is offline Offline
Junior Poster in Training

Re: class

  #6  
Aug 7th, 2007
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
Reply With Quote  
Join Date: Jul 2005
Posts: 1,157
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 152
Lerner Lerner is offline Offline
Veteran Poster

Re: class

  #7  
Aug 8th, 2007
>>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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:16 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC