Hey all,
Here I am again, most of my program written but I'm stuck with the last step.

The assignment is asking us novice programmers to add three functions (1. to subtract, 2. to multiply, 3. add a function DollarsAndCentsAre which will print out (using cout) the complete value of a money value) to a program using a class called MoneyType where we initialize two amounts of money and use those two in the functions.
There are three extra functions that were written for us that set dollars = to newDollars and cents = to newCents and then DollarsAre() and CentsAre() which return dollars and cents.

My problem lies with the DollarsAndCentsAre. I'm not quite sure what he's asking us to do here.. do you guys see it as putting the two together and then print out one value after the calcluations?

Here's the code:

// Program Money manipulates instances of class MoneyType.

class MoneyType {

public:

    void  Initialize(long, long);
    long  DollarsAre() const;
    long  CentsAre() const;
    MoneyType  Add(MoneyType) const;
	MoneyType Sub(MoneyType) const;
	MoneyType Multi(MoneyType) const;
	MoneyType DollarsAndCentsAre(MoneyType)const;


private:
    long  dollars;
    long  cents;
};

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

#include <iostream>

using namespace std;

int main () {
    char a_character;
    MoneyType  money1;
    MoneyType  money2;
    MoneyType  money3;
	MoneyType  money4;
	MoneyType  money5;
    money1.Initialize(10, 59);
    money2.Initialize(20, 70);
    money3 = money1.Add(money2);
	money4 = money1.Sub(money2);
	money5 = money1.Multi(money2);
	cout << "Added values are: " << "$"  << money3.DollarsAre() << "."  << money3.CentsAre()  << endl;
	cout << "Subtracted values are: " << "$" << money4.DollarsAre() << "." << money4.CentsAre() << endl;
	cout << "Multiplied values are: " << "$" << money5.DollarsAre() << "." << money5.CentsAre() << endl;
    cout << endl << "Hit any key and enter to exit: ";
    cin >> a_character;
    return 0;
}

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

void  MoneyType::Initialize(long newDollars, long newCents)
// Post: dollars is set to newDollars; cents is set to
//       newCents.
{
    dollars = newDollars;
    cents = newCents;
}

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

long  MoneyType::DollarsAre() const
// Post: Class member dollars is returned.
{
    return dollars;
}

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

long  MoneyType::CentsAre() const
// Post: Class member cents is returned.   
{
    return cents;
}

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

MoneyType MoneyType::Add(MoneyType  value) const
// Pre:  Both operands have been initialized. 
// Post: value + self is returned.
{
    MoneyType  result;
    result.dollars = dollars + value.dollars + (cents + value.cents) / 100;
    result.cents = (cents + value.cents) % 100;
    return result;
}

MoneyType MoneyType::Sub(MoneyType value) const
//Pre: Both operatnds have been initialized
//post: value - self is returned
{
	MoneyType result;
	result.dollars = value.dollars - dollars;
	result.cents = value.cents - cents;
		if(result.cents < 0)
		{
			if (result.dollars > 0)
			{
			result.dollars--;
			result.cents += 100;
			}
		}
		return result;
	
}

MoneyType MoneyType::Multi(MoneyType value) const
//Pre: Both operands have been initialized
//Post: value * self is returned
{
	MoneyType result;
	result.dollars = value.dollars * dollars;
	result.cents = (value.cents * cents) % 100;
	return result;
}

MoneyType MoneyType::DollarsAndCentsAre(MoneyType value) const
{

//??????

}

Thanks for your help and time
iTsweetie

Are you sure this is the right specification?

MoneyType MoneyType::DollarsAndCentsAre(MoneyType value) const

and are you sure it's supposed to only display something (i.e. not calculate something)?

If so, it seems to me that this should be the specification for a function that simply displays:

void MoneyType::DollarsAndCentsAre()

Why return anything? And why would you need a parameter besides this if all you are doing is displaying?

I imagine the function is this:

void MoneyType::DollarsAndCentsAre() 
{
    // display dollar sign.
    // display dollars.
    // display decimal point.
    // display cents.
}
Comments
Nicely put

OH! So instead of just using cout statements I should write the cout statement inside that function and just call it in main? That makes sense. Thanks for your help!! It's always so simple when it seems so difficult.

iTsweetie

OH! So instead of just using cout statements I should write the cout statement inside that function and just call it in main? That makes sense. Thanks for your help!! It's always so simple when it seems so difficult.

iTsweetie

I would imagine so. Follow the specification. If you are allowed to, I would rename the function to something like DisplayDollarsAndCents so it is more clear.


Actually, looking at main gives a greater appreciation of what you are supposed to do:

cout << "Added values are: " << "$"  << money3.DollarsAre() << "."  << money3.CentsAre()  << endl;

If these are the function calls, then this function isn't going to work:

long  MoneyType::DollarsAre() const
// Post: Class member dollars is returned.
{
    return dollars;
}

It should contain a cout statement, like DollarsAndCentsAre:

void  MoneyType::DollarsAre() const
// Post: Class member dollars is returned.
{
    cout << dollars;
}

That makes more sense based on the function calls from main, which I had not noticed before.


[EDIT]
Scratch that! My bad. What was I thinking?

cout << "Added values are: " << "$"  << money3.DollarsAre() << "."  << money3.CentsAre()  << endl;

As they are, they NEED to not be void functions and they WOULDN"T have cout statements, so ignore my suggestion above regarding making the DollarsAre () and CentsAre () functions void functions and ignore the idea that they should have cout statements. Sorry.

void MoneyType::DollarsAndCentsAre()

You should probably put the const back into this function since you aren't changing anything. I shouldn't have taken it out:

void MoneyType::DollarsAndCentsAre() const
This article has been dead for over six months. Start a new discussion instead.