hey guys. the following code should return a percentage of the current values of the object. I keep getting 0 returned. If this isn't enough code let me know.

Money test = ourAmount.percent ( 10 ) ;
test.output ( ) ;
const Money Money::percent ( int percentFigure )
{
    return ( percentFigure / 100 * dollars + percentFigure / 100 * cents ) ;
}
void Money::output( ) const
{
    int absDollars = abs(dollars);
    int absCents = abs(cents);
    if (dollars < 0 || cents < 0)//accounts for dollars == 0 or cents == 0
        cout << "$-";
    else
        cout << '$';
    cout << absDollars;

    if (absCents >= 10)
        cout << '.' << absCents;
    else
        cout << '.' << '0' << absCents;
}

Recommended Answers

All 8 Replies

I've narrowed it to here somehwere (i think)

dollars = dollars * ( percentFigure / 100 ) ;

you are using integers, so the program drops all fractions. percentFigure / 100 is 0 for all values of percentFigure less than 100. Change data types to float and it will work as you expected.

Member Avatar for iamthwee

Use a double.

Also you might want to consider using brackets () around statements such as percentFigure / 100 * dollars + percentFigure / 100 * cents to be extra sure it is evaluating the expression in the correct order.

Also change the single quotes for " in your cout statements . Although it is not wrong it keeps some consistency and you are less likely to make a boo boo. cout << ' 0 ';

you are using integers, so the program drops all fractions. percentFigure / 100 is 0 for all values of percentFigure less than 100. Change data types to float and it will work as you expected.

I had already tried this. It's still not outputting anything other than 0. However, I just tried doing 100/100 * dollars to see if it would work and it did output the correct answer for that equation. Where else should I have changed the int to double?

bump*

post curent code because I can't see your monitor from where I am sitting.

Roger.

// 0801.cpp
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

//Class for amounts of money in U.S. currency.
class Money
{
public:
    Money( );
    Money(double amount);
    Money(int theDollars, int theCents);
    Money(int theDollars);

    const Money percent ( int percentFigure ) ;

    double getAmount( ) const;
    int getDollars( ) const;
    int getCents( ) const;
    void input( ); //Reads the dollar sign as well as the amount number.
    void output( ) const;
private:
    double dollars; //A negative amount is represented as negative dollars and
    double cents; //negative cents. Negative $4.50 is represented as -4 and -50

    int dollarsPart(double amount) const;
    int centsPart(double amount) const;
    int round(double number) const;
};

const Money operator +(const Money& amount1, const Money& amount2);

const Money operator -(const Money& amount1, const Money& amount2);

bool operator ==(const Money& amount1, const Money& amount2);

const Money operator -(const Money& amount);

/************************
 operation overloading declaration data for ch8.prob1
************************/
bool operator < (const Money & amount1 , const Money & amount2 ) ;

bool operator > (const Money & amount1 , const Money & amount2 ) ;

bool operator <= (const Money & amount1 , const Money & amount2 ) ;

bool operator >= (const Money & amount1 , const Money & amount2 ) ;
/******************************************************************/




#include "prob1.h"

int main( )
{
    Money yourAmount, myAmount(10, 9);
    cout << "Enter an amount of money: ";
    yourAmount.input( );

    cout << "Your amount:  "; 
    yourAmount.output( ); 
    cout << endl;
    cout << "My amount:  "; 
    myAmount.output( ); 
    cout << endl;

    /************************
     Test data for ch8.prob1
    ************************/
    if ( yourAmount <= myAmount )
        cout << "\nYour amount is less than or equal to my amount.\n" ;
    else if ( yourAmount >= myAmount )
        cout << "\nMy amount is less than or equal to your amount.\n" ;

    if ( yourAmount < myAmount )
        cout << "To be more precise, I have more money than you.\n\n" ;
    else if ( yourAmount > myAmount )
        cout << "To be more precise, you have more money than me.\n\n" ;
    else
        cout << "To be more precise, we have the same amount of money.\n\n" ;
    /*************************************************************************/

    Money ourAmount = yourAmount + myAmount;
    yourAmount.output( ); cout << " + "; myAmount.output( ); 
    cout << " equals "; ourAmount.output( ); cout << endl;

    Money diffAmount = yourAmount - myAmount;
    yourAmount.output( ); cout << " - "; myAmount.output( ); 
    cout << " equals "; diffAmount.output( ); cout << endl;

    Money test = ourAmount.percent ( 10 ) ;
    test.output ( ) ;

    return 0;
}




#include "prob1.h"

const Money operator +(const Money& amount1, const Money& amount2)
{
    int allCents1 = amount1.getCents( ) + amount1.getDollars( )*100;
    int allCents2 = amount2.getCents( ) + amount2.getDollars( )*100;
    int sumAllCents = allCents1 + allCents2;
    int absAllCents = abs(sumAllCents); //Money can be negative.
    int finalDollars = absAllCents/100;
    int finalCents = absAllCents%100;

    if (sumAllCents < 0)
    {
        finalDollars = -finalDollars;
        finalCents = -finalCents;
    }

    return Money(finalDollars, finalCents);
}

//Uses cstdlib:
const Money operator -(const Money& amount1, const Money& amount2)
{
    int allCents1 = amount1.getCents( ) + amount1.getDollars( )*100;
    int allCents2 = amount2.getCents( ) + amount2.getDollars( )*100;
    int diffAllCents = allCents1 - allCents2;
    int absAllCents = abs(diffAllCents); 
    int finalDollars = absAllCents/100;
    int finalCents = absAllCents%100;

    if (diffAllCents < 0)
    {
        finalDollars = -finalDollars;
        finalCents = -finalCents;
    }

    return Money(finalDollars, finalCents);
}

bool operator ==(const Money& amount1, const Money& amount2)
{
    return ( amount1.getDollars( ) == amount2.getDollars( ) ) && ( amount1.getCents ( ) == amount2.getCents( ) ) ;
}

/************************
operation overloading deffinition data for ch8.prob1
************************/
bool operator < ( const Money & amount1 , const Money & amount2 )
{
 return ( amount1.getDollars ( ) < amount2.getDollars ( ) ) || ( amount1.getDollars ( ) == amount2.getDollars ( ) ) && ( amount1.getCents ( ) < amount2.getCents ( ) ) ;
}

bool operator > ( const Money & amount1 , const Money & amount2 )
{
 return ( amount1.getDollars ( ) > amount2.getDollars ( ) ) || ( amount1.getDollars ( ) == amount2.getDollars ( ) ) && (amount1.getCents ( ) > amount2.getCents ( ) ) ;
}

bool operator <= ( const Money & amount1 , const Money & amount2 )
{
    /* pre-finalization coding */

    //if ( amount1.getDollars ( ) < amount2.getDollars ( ) )
    //    return true ;
    //else if ( amount1.getDollars ( ) == amount2.getDollars ( ) && amount1.getCents ( ) < amount2.getCents ( ) )
    //    return true ;
    //else if ( amount1.getDollars ( ) == amount2.getDollars ( ) && amount1.getCents ( ) == amount2.getCents ( ) )
    //    return true ;
    //else
    //    return false ;

    /***********************************************/

    return ( amount1.getDollars ( ) < amount2.getDollars ( ) ) || 
        ( amount1.getDollars ( ) == amount2.getDollars ( ) && amount1.getCents ( ) < amount2.getCents ( ) ) || 
        amount1.getCents ( ) == amount2.getCents ( ) ;
}

bool operator >= ( const Money & amount1 , const Money & amount2 )
{
    /* pre-finalization coding */

    //if ( amount1.getDollars ( ) > amount2.getDollars ( ) )
    //    return true ;
    //else if ( amount1.getDollars ( ) == amount2.getDollars ( ) && amount1.getCents ( ) > amount2.getCents ( ) )
    //    return true ;
    //else if ( amount1.getDollars ( ) == amount2.getDollars ( ) && amount1.getCents ( ) == amount2.getCents ( ) )
    //    return true ;
    //else
    //    return false ;

    /***********************************************/

    return ( amount1.getDollars ( ) > amount2.getDollars ( ) ) || 
        ( amount1.getDollars ( ) == amount2.getDollars ( ) && amount1.getCents ( ) > amount2.getCents ( ) ) || 
        amount1.getCents ( ) == amount2.getCents ( ) ;
}
/****************************************************************************/

const Money operator -(const Money& amount)
{
    return Money(-amount.getDollars( ), -amount.getCents( ));
}

Money::Money( ): dollars(0), cents(0)
{/*Body intentionally empty.*/}

Money::Money(double amount)
              : dollars(dollarsPart(amount)), cents(centsPart(amount))
{/*Body intentionally empty*/}

Money::Money(int theDollars)
              : dollars(theDollars), cents(0)
{/*Body intentionally empty*/}

//Uses cstdlib:
Money::Money(int theDollars, int theCents)
{
    if ((theDollars < 0 && theCents > 0) || (theDollars > 0 && theCents < 0))
    {
        cout << "Inconsistent money data.\n";
        exit(1);
    }
    dollars = theDollars;
    cents = theCents;
}

const Money Money::percent ( int percentFigure )
{
    cout << dollars << "." << cents << endl ;
    dollars = dollars * ( 10 / 100 ) ;
    cout << dollars << endl ;
    cents = ( 10 / 100 ) * cents ;

    cout << dollars << cents << endl ;

    return 2 ;
}

double Money::getAmount( ) const
{
    return (dollars + cents*0.01);
}

int Money::getDollars( ) const
{
    return dollars;
}

int Money::getCents( ) const
{
    return cents;
}

//Uses iostream and cstdlib:
void Money::output( ) const
{
    int absDollars = abs(dollars);
    int absCents = abs(cents);
    if (dollars < 0 || cents < 0)//accounts for dollars == 0 or cents == 0
        cout << "$-";
    else
        cout << '$';
    cout << absDollars;

    if (absCents >= 10)
        cout << '.' << absCents;
    else
        cout << '.' << '0' << absCents;
}

//Uses iostream and cstdlib:
void Money::input( )
{
    char dollarSign;
    cin >> dollarSign; //hopefully
    if (dollarSign != '$')
    {
        cout << "No dollar sign in Money input.\n";
        exit(1);
    }

    double amountAsDouble;
    cin >> amountAsDouble;
    dollars = dollarsPart(amountAsDouble);
    cents = centsPart(amountAsDouble);
}

int Money::dollarsPart(double amount) const
{
    return static_cast<int>(amount);
}

int Money::centsPart(double amount) const
{
    double doubleCents = amount*100;
    int intCents = (round(fabs(doubleCents)))%100;//% can misbehave on negatives
    if (amount < 0)
        intCents = -intCents;
    return intCents;
}

int Money::round(double number) const
{
    return static_cast<int>(floor(number + 0.5));
}

I don't see where you declared the class variables.
>>line 130
>>dollars = dollars * ( 10 / 100 ) ;

What is that supposed to do ? That is still integer math which will result in 0. 10 is an integer and 100 another integer, Code it like this and it will work

dollars *= 0.1

or 
dollars /= 10.0;

Of course dollars must be declared as either float or double if you want to see the fractional parts.

what is line 175 supposed to do? If you want dollarSign why not just hardcode it? People don't normally require the dollar sign as part of the input, only the digits and possibly negative sign.

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.