Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I've been working on this program for approximately 2 weeks and I just can't do it, I'm a beginner it's really difficult for me and I don't know what to do anymore. The part where you supposed to get your cents back doesn't want to work and I really don't know why, I was trying to optimize as best as I can my code to have less code line but I'm not capable to do it. Some help would be very appreciate please.
Many Thanks in Advance.

C++
#include <iostream>
#include <math.h>
using namespace std;

int main() 
{
	double iAmount_due { 0 };
	double iGiven_money { 0 };
	double iMoney_back;	

	iMoney_back = iGiven_money - iAmount_due;

	int iMoney100 { 0 };
	int iMoney50  { 0 };
	int iMoney20  { 0 };
	int iMoney10  { 0 };
	int iMoney5   { 0 };
	int iMoney2   { 0 };
	int iMoney1   { 0 };
	int iCent25   { 0 }; 
	int iCent10   { 0 };
	int iCent5    { 0 };
	int iCent1    { 0 };

    cout << "Enter the amount due please: " << endl;

	cin >> iAmount_due;

    cout << "Enter the amount given please: " << endl;

	cin >> iGiven_money;


	if (iGiven_money >= iAmount_due) {

	   iMoney_back = iGiven_money - iAmount_due;

	   cout << "We will give you : " << iMoney_back << " $ back" << endl;

	}
	else {	
	   cout << "No money back" << endl;	
	}
       
	while (iGiven_money >= iAmount_due) {
	
		iMoney100 = iMoney_back / 100;
		iMoney_back = (int)iMoney_back % 100;

		cout << "You will get: " << iMoney100 << " X 100 $ " << endl;
	
		iMoney50 = iMoney_back / 50;
		iMoney_back = (int)iMoney_back % 50;

		cout << "You will get: " << iMoney50 << " X 50 $ " << endl;

		iMoney20 = iMoney_back / 20;
		iMoney_back = (int)iMoney_back % 20;

		cout << "You will get: " << iMoney20 << " X 20 $ " << endl;

		iMoney10 = iMoney_back / 10;
		iMoney_back = (int)iMoney_back % 10;

		cout << "You will get: " << iMoney10 << " X 10 $ " << endl; 

		iMoney5 = iMoney_back / 5;
		iMoney_back = (int)iMoney_back % 5;

		cout << "You will get: " << iMoney5 << " X 5 $ " << endl; 

		iMoney2 = iMoney_back / 2;
		iMoney_back = (int)iMoney_back % 2;

		cout << "You will get: " << iMoney2 << " X 2 $ " << endl; 
	
	    iMoney1 = iMoney_back / 1;
		iMoney_back = (int) iMoney_back % 1;

		cout << "You will get: " << iMoney1 << " X 1 $ " << endl;

		iCent25 = iMoney_back / 0.25;
		iMoney_back = fmod ((int)iMoney_back, 0.25);

		cout << "You will get: " << iCent25 << " X 0.25 $ " << endl;

		iCent10 = iMoney_back / 0.10;
		iMoney_back = fmod ((int)iMoney_back , 0.10);

		cout << "You will get: " << iCent10 << " X 0.10 $ " << endl;

		iCent5 = iMoney_back / 0.5;
		iMoney_back = fmod ((int)iMoney_back , 0.5);

		cout << "You will get: " << iCent5 << " X 0.5 $ " << endl;

		iCent1 = iMoney_back / 0.1;
		iMoney_back = fmod ((int)iMoney_back, 0.1);

		cout << "You will get: " << iCent1 << " X 0.1 $ " << endl;

		break;	
	}

	return 0;
}


What I have tried:

I tried everything that I could for 2 weeks, I tried many c++ library to solve my problem with decimals my program still don't give me back the cents
Posted
Updated 31-Jan-20 22:37pm
v2
Comments
KarstenK 1-Feb-20 4:39am    
you missed to subtract the back money pieces from iMoney_back :-O

the other answers left nothing more to writte.

I am not going to rewrite this for you but I think it would be easier for you to think in terms of cents as the 'whole' number. What this means is you should accept the input of the amount in dollars with cents as the fractional part as you do now. Then you should make another variable that is an integer and it is the input value times 100. For example, if you enter 123.45 then this new variable will hold 12345. The do all of your calculations using this variable and it will be all integer math.

The reasoning behind this is cents are essentially a whole number. With money, there can be no smaller value so use it as the base unit. One hundred dollars is 10000 cents, ten dollars is 1000 cents, and one dollar is 100 cents. After that, the coin denominations are whole values. This will simplify things a lot and should be easier for you to deal with. The first section of your code should change very little - just multiply the divisors by 100. The last part will also scale the divisors by 100 but it should be changed to use integer math.

One tip I will give is about how you compute your values. Here is one excerpt :
C++
iMoney50 = iMoney_back / 50;
iMoney_back = (int)iMoney_back % 50;
You can make this a function to simplify things. The key is to pass the amount of money in as a reference since it will be modified in the function. Here is that code :
C++
int GetTypeAmount( int & money, int denomination )
{
    int count = money / denomination;
    money %= denomination;
    return count;
}
This will return the number of that denomination of money. Here is a sequence of calls to it :
C++
int count = 0;
int money = 12345;
count = GetTypeAmount( money, 10000 );
cout << "You will get: " << count << " X 100.00 $" << endl;
count = GetTypeAmount( money, 5000 );
cout << "You will get: " << count << " X  50.00 $" << endl;
count = GetTypeAmount( money, 2000 );
cout << "You will get: " << count << " X  20.00 $" << endl;
count = GetTypeAmount( money, 1000 );
cout << "You will get: " << count << " X  10.00 $" << endl;
count = GetTypeAmount( money, 500 );
cout << "You will get: " << count << " X   5.00 $" << endl;
count = GetTypeAmount( money, 100 );
cout << "You will get: " << count << " X   1.00 $" << endl;
count = GetTypeAmount( money, 25 );
cout << "You will get: " << count << " X   0.25 $" << endl;
count = GetTypeAmount( money, 10 );
cout << "You will get: " << count << " X   0.10 $" << endl;
count = GetTypeAmount( money, 5 );
cout << "You will get: " << count << " X   0.05 $" << endl;
count = money;
cout << "You will get: " << count << " X   0.01 $" << endl;
You could take this one step farther and include the output in the function but I will leave that for you to figure out.
 
Share this answer
 
You are doing integer mod on floating point values, which results in an integer, so if iMoney_back = 2.34, then (int)IMoney_back % 100 evaluates to (int)2, not (double)2.34

Other than that, here's some things I would be commenting on as your instructor

1) there's a lot of "repeated code" here, where you're doing the almost same thing over and over again. If you've talked about functions (sometimes called methods or sub-routines), maybe you could think about how you might put the similar bits into a sub-routine.

2)You get a lot of messages about getting 0 denomination values back. Maybe you can think of a way to only print out the change you will actually receive?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900