GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Jun-2008, 09:39
VulturEMaN VulturEMaN is offline
New Member
 
Join Date: May 2008
Posts: 6
VulturEMaN is on a distinguished road

Need help with classes and overloading


Ok...I'm totally fried. I pretty much finished my program, minus 1 small class that I'm having trouble trying to figure out how to implement it (Money::percent) near the bottom of the code.

CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;

class Money
{
public:
  friend bool operator >(const Money& amount1, const Money& amount2);
  friend bool operator >=(const Money& amount1, const Money& amount2);
  Money(long dollars, int cents);
  Money(long dollars);
  Money( );
  int percent(int percent_figure) const;
  friend istream& operator >>(istream& ins, Money& amount);
  friend ostream& operator <<(ostream& outs, const Money& amount);

private:
  long all_cents;
};

int digit_to_int(char c);

int main( ){
  Money amount1, amount2;
  int percent;

  cout << "Enter two amounts of money using dollar sign and decimal point ";
  cout << "(e.g., $12.57)" << endl << endl;

  cout << "First amount: ";
  cin >> amount1;
  cout << "Second amount: ";
  cin >> amount2;

  // Test > and >=
  if (amount1 > amount2)
    cout << amount1 << " > " << amount2 << endl;
  else if (amount1 >= amount2)
    cout << amount1 << " = " << amount2 << endl;
  else
    cout << amount1 << " < " << amount2 << endl;

  // Test percent
  cout << endl << "Enter a percent (0-100): ";
  cin >> percent;
  cout << percent << "% of " << amount1 << " is " << amount1.percent(percent);

  cout << endl << endl << "Bye!" << endl;
  system("pause");
}

//Uses iostream, cctype, cstdlib:
istream& operator >>(istream& ins, Money& amount){
  char one_char, decimal_point, digit1, digit2; //digits for the amount of cents
  long dollars;
  int cents;
  bool negative; //set to true if input is negative.

  ins >> one_char;
  if (one_char == '-'){
    negative = true;
    ins >> one_char; //read '$'
    }
  else
    negative = false; //if input is legal, then one_char == '$'

  ins >> dollars >> decimal_point >> digit1 >> digit2;
  if (one_char != '$' || decimal_point != '.' || !isdigit(digit1) || !isdigit(digit2)){
    cout << "Error illegal form for money input\n";
    exit(1);
    }

    cents = digit_to_int(digit1)*10 + digit_to_int(digit2);
    amount.all_cents = dollars*100 + cents;
    if (negative)
      amount.all_cents = -amount.all_cents;
    return ins;
}

int digit_to_int(char c){
  return (int(c) - int('0'));
}

ostream& operator <<(ostream& outs, const Money& amount){
  long positive_cents, dollars, cents;
  positive_cents = labs(amount.all_cents);
  dollars = positive_cents/100;
  cents = positive_cents%100;

  if (amount.all_cents < 0)
    outs << "-$" << dollars << '.';
  else
    outs << "$" << dollars << '.';

  if (cents < 10)
    outs << '0';
  outs << cents;
  return outs;
}

Money::Money(long dollars, int cents){
  if(dollars*cents < 0) //If one is negative and one is positive
    {
    cout << "Illegal values for dollars and cents.\n";
    exit(1);
    }
  all_cents = dollars*100 + cents;
}

int Money::percent(int percent_figure) const{
  cout << amount1(percent_figure/100));
}

Money::Money(long dollars) : all_cents(dollars*100){/*Body intentionally blank.*/}
Money::Money() : all_cents(0){/*Body intentionally blank.*/}
bool operator >(const Money& amount1, const Money& amount2){/*Body intentionally blank.*/}
bool operator >=(const Money& amount1, const Money& amount2){/*Body intentionally blank.*/}
  #2  
Old 09-Jun-2008, 10:28
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need help with classes and overloading


From what I can see, the variable amount1 is not known to class Money. It is an instance of class Money whose scope is in main(). I think what you want is this:

CPP / C++ / C Code:
int Money::percent( int percent_figure )
{
  return all_cents*percent_figure/100;
}
  #3  
Old 09-Jun-2008, 12:30
VulturEMaN VulturEMaN is offline
New Member
 
Join Date: May 2008
Posts: 6
VulturEMaN is on a distinguished road

Re: Need help with classes and overloading


yes thank you that was very similar to what i wanted. i had to change it to
return ((all_cents*(percent_figure/100))/100);
so that i could have it in decimal form.

i was overcomplicating the program by trying to do random things that weren't needed.
  #4  
Old 09-Jun-2008, 13:23
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need help with classes and overloading


If you do that, you need to change the data type to float/double because any calculation that results between 0 and 1 (not including 1) as an integer will be 0.
  #5  
Old 09-Jun-2008, 18:21
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Need help with classes and overloading


What problem are you actually having ?
  #6  
Old 09-Jun-2008, 21:45
VulturEMaN VulturEMaN is offline
New Member
 
Join Date: May 2008
Posts: 6
VulturEMaN is on a distinguished road

Re: Need help with classes and overloading


Quote:
Originally Posted by fakepoo
If you do that, you need to change the data type to float/double because any calculation that results between 0 and 1 (not including 1) as an integer will be 0.

yea i added:

CPP / C++ / C Code:
double Money::percent(double percent_figure) const{
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(2);
  return ((all_cents*(percent_figure/100))/100);
  }
  #7  
Old 09-Jun-2008, 23:44
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Need help with classes and overloading


Quote:
Originally Posted by VulturEMaN
CPP / C++ / C Code:
return ((all_cents*(percent_figure/100))/100);
This is not sufficient. Because you are specifying division by 100 (as an integer value...), you are asking that integer division be performed before converting the result to a double. You should at least change the constant used in division to 100. (as a floating-point value...) which will force the expression to be computed as a floating-point value. As written, information is being lost.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help/Corrections in Classes & Operators Overloading MKQ C++ Forum 3 29-Jan-2008 09:03
overloading normal & virtual functions balusss C++ Forum 5 25-Aug-2007 10:39
What's good to implement operator overloading with 'friend'? meili100 C++ Forum 2 22-Jul-2007 08:34
Overloading operator = in a managed class Gix .NET Forum 5 26-Nov-2006 05:34
A Comprehensive Digest of C++ mithunjacob C++ Forum 39 20-Jun-2004 20:09

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:44.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.