Help Adding Dollars and Cents Overloaded Function

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 8505 Views - Last Post: 11 August 2015 - 03:56 PM Rate Topic: -----

#1 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Help Adding Dollars and Cents Overloaded Function

Posted 10 August 2015 - 04:59 PM

Okay, here is my source code:
#include <iostream>

//Class prototype
class Cents;

class Dollars
{
	private:
		int m_DollarAmount;
	
	public:
		Dollars(int dollars = 1) { m_DollarAmount = dollars; }
		
		void getDollar()
		{
			int x;
			std::cin >> x;
			void setDollar(int x);
		}
		
		void setDollar()
		{
			int y;
			std::cin >> y;
			m_DollarAmount = y;
		}
		
		//Overload to add Dollars and Cents
		friend Dollars operator+(const Dollars &d1, const Cents &c1);
		friend Dollars operator+(const Cents &c1, const Dollars &d1);
};

class Cents
{
	private:
		float m_CentsAmount;
	
	public:
		Cents(float cents = 25) { m_CentsAmount = cents; }
		
		void getCents()
		{
			int cents;
			std::cin >> cents;
			void setCents(int cents);
		}
		
		void setCents()
		{
			int cents;
			std::cin >> cents;
			m_CentsAmount = cents;
		}
		
		friend Cents operator+(const Dollars &d1, const Cents &c1);
		friend Cents operator+(const Cents &c1, const Dollars &d1);
};

Dollars operator+(const Dollars &d1, const Cents &c1)
{
	//Confused here on how to add these correctly.
}

Dollars operator+(const Cents &c1, const Dollars &d1)
{
	//Confused here on how to add these correctly.
}

Cents operator+(const Cents &c1, const Dollars &d1)
{
	//Confused here on how to add these correctly.
}

Cents operator+(const Dollars &d1, const Cents &c1)
{
	//Confused here on how to add these correctly.
}

int main()
{
	//Initiating objects
	Dollars UserCash;
	Cents UserCoin;
	
	std::cout << "Please enter the amount of dollars you have: ";
	UserCash.getDollar();
	
	std::cout << "Please enter the amount of cents you have: ";
	UserCoin.getCents();
	
	return 0;
}



It compiles. As of now there are no errors. I am confused on how I would add my cents to my dollars and have them wrap around at every dollar. For example if I enter one dollar and 250 cents, I want it to add to 3.50 cents. I don't know how I would do this. I don't want an answer, but a push in the right direction to work this out please!

Also, is my implementation correct on adding different types for the overloaded functions?

Thanks,
Chris

Is This A Good Question/Topic? 0
  • +

Replies To: Help Adding Dollars and Cents Overloaded Function

#2 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Help Adding Dollars and Cents Overloaded Function

Posted 10 August 2015 - 07:31 PM

I'd store everything as integers of pennies, do all math using those, then use printf to print that in dollars and cents:

int money = 1025;
float dollarsCents =  money / 100.0;


I'll leave it to you to get printf to print $10.25

Note that you could also use sprintf if you want to store it as a string for later use.
Was This Post Helpful? 0
  • +
  • -

#3 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1064
  • View blog
  • Posts: 4,105
  • Joined: 13-June 14

Re: Help Adding Dollars and Cents Overloaded Function

Posted 10 August 2015 - 09:38 PM

On lines 18 and 45, you have declared a function prototype inside another function. Are you sure that's what you wanted to do?
Was This Post Helpful? 0
  • +
  • -

#4 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: Help Adding Dollars and Cents Overloaded Function

Posted 10 August 2015 - 11:11 PM

View Postndc85430, on 10 August 2015 - 09:38 PM, said:

On lines 18 and 45, you have declared a function prototype inside another function. Are you sure that's what you wanted to do?


I suppose I could have went with:
void getDollar()
        {
            int x;
            std::cin >> x;
            m_DollarAmount = x;
        }



Same for the other function of course. Save myself some space in the program. When things get more advanced, this would be a good thing not to goof on. Thanks!

CT, when I get to work tomorrow, I'm going to give it a shot and post the new code up here if I can get somewhere with it. Thanks man.

View PostCTphpnwb, on 10 August 2015 - 07:31 PM, said:

I'd store everything as integers of pennies, do all math using those, then use printf to print that in dollars and cents:

int money = 1025;
float dollarsCents =  money / 100.0;


I'll leave it to you to get printf to print $10.25

Note that you could also use sprintf if you want to store it as a string for later use.


Oh, am I wrong in thinking sprintf is a C function? If so, I don't want to get into that, unless it's good practice to know. Please, correct me if I'm wrong.

This post has been edited by ChrispyChris: 10 August 2015 - 11:10 PM

Was This Post Helpful? 0
  • +
  • -

#5 Xupicor   User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: Help Adding Dollars and Cents Overloaded Function

Posted 11 August 2015 - 12:24 AM

Well, it is from C standard library, so it works on C strings (null-terminated char arrays). If you'd rather use C++ standard strings and streams - you can convert a number to a string using std::stringstream, you might also want to look at std::setprecision() in <iomanip> and precision() member of ios_base.

Or you can use std::to_string() if you like the formatting arguments of printf() and C++ standard strings brought together. +1 Anarion for this particular tip. ; )

This post has been edited by Xupicor: 11 August 2015 - 01:45 PM

Was This Post Helpful? 0
  • +
  • -

#6 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: Help Adding Dollars and Cents Overloaded Function

Posted 11 August 2015 - 10:53 AM

*Delete post*

This post has been edited by ChrispyChris: 11 August 2015 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: Help Adding Dollars and Cents Overloaded Function

Posted 11 August 2015 - 11:18 AM

Okay I think I got this part figured out, but would still appreciate some clarification.

//Class prototype
class Cents;

class Dollars
{
	private:
		int m_DollarAmount;
	
	public:
		Dollars(int dollars = 1) { m_DollarAmount = dollars; }
		
		const int getDollar()
		{
			return m_DollarAmount;
		}
		
		void userSetDollar()
		{
			int y;
			std::cin >> y;
			m_DollarAmount = y;
		}
		
		//Overload to add Dollars and Cents
		friend Dollars operator+(const Dollars &d1, const Cents &c1);
		friend Dollars operator+(const Cents &c1, const Dollars &d1);
};

class Cents
{
	private:
		int m_CentsAmount;
	
	public:
		Cents(int cents = 25) { m_CentsAmount = cents; }
		
		const int getCents()
		{
			return m_CentsAmount;
		}
		
		void userSetCents()
		{
			int cents;
			std::cin >> cents;
			m_CentsAmount = cents;
		}
		
		/*Overload to add Dollars and Cents
		friend Cents operator+(const Dollars &d1, const Cents &c1);
		friend Cents operator+(const Cents &c1, const Dollars &d1);*/
		friend Dollars operator+(const Dollars &d1, const Cents &c1);
		friend Dollars operator+(const Cents &c1, const Dollars &d1);
};



Those are my classes. I am guessing that I need to just friend the Dollar operator overload in my cents class, instead of create my own Cents overloaded function. Now I'm allowed to work on the variables I wanted to. Could someone explain this to me a little more please, like why I don't have to overload a Cents operator+ and just friend the Dollar one? I guess for some reason I thought I would have to create Cents's own overloads, but that's false?

Oh, I commented out the Cents operator+ which is why it now works, so that's what I'm curious about.
Was This Post Helpful? 0
  • +
  • -

#8 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: Help Adding Dollars and Cents Overloaded Function

Posted 11 August 2015 - 11:53 AM

Well, an overloaded operator+ is still a function, right? When you overload functions, you can't have two different definitions that are the same in arguments and only differ in the return type. The case is like the following:
int function();
string function(); //error!

You have to realize that you are overloading the global operator+ set of functions and therefore, the above overloading rule is applicable.

Quote

you can convert a number to a string using std::stringstream

Since C++11 there is another option available: to_string. You don't need a std::stringstream just for being able to convert, anymore! Isn't that cool? ;)
Spoiler

This post has been edited by Anarion: 11 August 2015 - 11:54 AM

Was This Post Helpful? 2
  • +
  • -

#9 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: Help Adding Dollars and Cents Overloaded Function

Posted 11 August 2015 - 12:04 PM

I understand now, thank you Anarion. I also appreciate that to string. In my current program I don't need it as this is just some practice stuff, but I suppose I can throw it in just for shits and gigs after I finish this up.

I'm here now:
#include <iostream>
#include <iomanip>

//Class prototype
class Cents;

class Dollars
{
	private:
		int m_DollarAmount;
	
	public:
		Dollars(int dollars = 2) { m_DollarAmount = dollars * 100.0; }
		
		const int getDollar()
		{
			return m_DollarAmount;
		}
		
		void userSetDollar()
		{
			int y;
			std::cin >> y;
			m_DollarAmount = y * 100;
		}
		
		//Overload to add Dollars and Cents
		friend Dollars operator+(const Dollars &d1, const Cents &c1);
		friend Dollars operator+(const Cents &c1, const Dollars &d1);
};

class Cents
{
	private:
		int m_CentsAmount;
	
	public:
		Cents(int cents = 25) { m_CentsAmount = cents; }
		
		const int getCents()
		{
			return m_CentsAmount;
		}
		
		void userSetCents()
		{
			int cents;
			std::cin >> cents;
			m_CentsAmount = cents;
		}
		
		//Overload to add Dollars and Cents
		friend Dollars operator+(const Dollars &d1, const Cents &c1);
		friend Dollars operator+(const Cents &c1, const Dollars &d1);
};

Dollars operator+(const Dollars &d1, const Cents &c1)
{
	return Dollars(d1.m_DollarAmount + c1.m_CentsAmount);
}

Dollars operator+(const Cents &c1, const Dollars &d1)
{
	return Dollars(c1.m_CentsAmount + d1.m_DollarAmount);
}

int main()
{
	//Initiating objects
	Dollars UserCash;
	Cents UserCoin;
	
	std::cout << "Please enter the amount of dollars you have: ";
	UserCash.userSetDollar();
	
	std::cout << "Please enter the amount of cents you have: ";
	UserCoin.userSetCents();
	
	float TotalAmount = Dollars(UserCash + UserCoin).getDollar() / 100.0;
	std::cout << std::fixed << std::setprecision(2) << TotalAmount;
	
	return 0;
}



I enter 2 (amount of dollars I have) and 50 (amount of cents I have) and 250.00 prints. Not sure how to get this fixed. Maybe a little hint :)/>/>?

Edit: Is dividing by 10000.0 the best way to go about this or is there a way I can set the decimal in the correct spot a more efficient and professional way?

This post has been edited by ChrispyChris: 11 August 2015 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#10 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: Help Adding Dollars and Cents Overloaded Function

Posted 11 August 2015 - 12:21 PM

I would suggest to move the division by 100.0 to getDollar() function. This way, the value you get is really in dollars, rather than cents (the name suggests that it should return dollar value).
Also, you could change from:
const int getDollar() { /*...*/ }

To this:
int getDollar() { /*...*/ }

The const in return type here (in this context) isn't practical.
Was This Post Helpful? 0
  • +
  • -

#11 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: Help Adding Dollars and Cents Overloaded Function

Posted 11 August 2015 - 12:32 PM

I did move it, that was a good tip, thank you. Now you mean to remove const because now I'm dividing in the function right? If you mean as it was before I put the division in the function I was taught that getter functions should be const and realistically if a function isn't changing a variable it should be const. Is this incorrect teachings?

Oh, why am I having to divide by 10000.0? 100 doesn't work, but in my head it should. Is there a way to fix this?
Was This Post Helpful? 0
  • +
  • -

#12 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1064
  • View blog
  • Posts: 4,105
  • Joined: 13-June 14

Re: Help Adding Dollars and Cents Overloaded Function

Posted 11 August 2015 - 12:34 PM

View PostChrispyChris, on 11 August 2015 - 08:32 PM, said:

I was taught that getter functions should be const and realistically if a function isn't changing a variable it should be const. Is this incorrect teachings?


Yes, this is sensible. However, you make the function const, not the return value, i.e.

int getDollar() const {
    ...
}

This post has been edited by ndc85430: 15 August 2015 - 05:42 AM

Was This Post Helpful? 1
  • +
  • -

#13 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: Help Adding Dollars and Cents Overloaded Function

Posted 11 August 2015 - 12:37 PM

:oops:
Was This Post Helpful? 0
  • +
  • -

#14 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: Help Adding Dollars and Cents Overloaded Function

Posted 11 August 2015 - 12:44 PM

View Postndc85430, on 12 August 2015 - 12:04 AM, said:

Yes, this is sensible. However, you make the function const, not the return value, i.e.

int getDollar() const {
...
}

Yes, ndc85430 is right. This way, you can imagine that the function is promising not to modify non-mutable members of that class or call functions that do so.
The const in return type, however, is a little bit different than this scenario. In the right context, it has a good usage but for now, you don't need the return const.

This post has been edited by Anarion: 11 August 2015 - 12:46 PM

Was This Post Helpful? 1
  • +
  • -

#15 ChrispyChris   User is offline

  • D.I.C Addict
  • member icon

Reputation: 46
  • View blog
  • Posts: 544
  • Joined: 06-December 10

Re: Help Adding Dollars and Cents Overloaded Function

Posted 11 August 2015 - 12:48 PM

Thanks guys, I am just curious about this last piece of the entire thing: why do I have to divide by 10000.0 to hit my decimal in the right place? I thought 100.0 would work. Also, is there a better way to go about doing that?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2