Displaying a claculation

Hi, i've tried searching here and online for an answer and couldn't seem to find one maybe you could help:
I have an assignment creating a kind of change calculator; you insert the sum of money and then get a display of how the bills and coins are divided, with priority to the bigger bills.
For example: your sum of money is 278
your should get: 278= 100*2+50*1+20*1+10*0+5*1+2*1 +1*1

I got the syntax and the logic on how to get there I just can't seem to make it appear like the formation above("278=100*2+50*2" etc...), all I get is the original sum(278 in this case).
I've tried putting "" on the math actions and leaving the variables out of them but it gives me an error.
Code included below would for some help! thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using namespace std;
int main()
{
	


	int sum, change, a,b,c,d,e,f,g;
	 

	cout << "please enter sum of money" << endl; 
	cin >> sum;
	a = sum/100%10000 ; 
	b = (sum - (a * 100)) / 50 % 1000;
	c = (sum - (a * 100) - (b * 50)) / 20 % 1000;
	d = (sum - (a * 100) - (b * 50) - (c * 20)) / 10 % 1000; 
	e = (sum - (a * 100) - (b * 50) - (c * 20) - (d * 10)) / 5 % 100; 
	f = (sum - (a * 100) - (b * 50) - (c * 20) - (d * 10) - (e * 5)) / 2 % 100;
	g = (sum - (a * 100) - (b * 50) - (c * 20) - (d * 10) - (e * 5) - (f * 2)) / 1 % 100; 
	change = (100 * a) + (50 * b)+ (20 * c) + (10 * d) + (5 * e) + (2 * f) + (1 * g);

 
	cout << "your bills divide as such:" << change << endl;
	
		return 0;
}
nHundreds = sum / 100; // integer division will give you 2
sum -= nHundreds * 100; // reducing sum to 78

nFifties = sum / 50;
sum -= nFifties * 50;

And so on....
you are summing all the values back together before displaying them.
you will need to print it like so:

cout << sum << " = 100 * " << a << " + 50 * " << b << " + 20 * " << c << " + 10 * " << d << " + 5 * " << e << " + 2 * " << f << " + 1 * " << g << endl;


the reason is that your * and + are being read as math that needs to be done befoe printing the result. to print the symbols * and + you need to print them as a character, which is why they are encased in the quotes "".

also, line 19 and the variable "change", is unnecessary as it is just summing everything back to the original input "sum".
Last edited on
Thank you so much, it's working great now!
glad i could help.
im very new to C++ so this might not be the best way of doing things. hopefully somebody more experienced can help optimize this if it is possible.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int multiple(int amount, int denomination)
{
    cout << amount / denomination << '*' << denomination << " ";
    return amount % denomination;
}

int main(){
    
    int amount = 0;
    int denom_array[]{100, 50, 20, 10, 5, 2, 1};
    
    cout << "Please enter amount: ";
    cin >> amount;
    
    for(int i = 0; i < sizeof(denom_array)/sizeof(int); i++)
        amount = multiple(amount, denom_array[i]);
    
    return 0;
}


Please enter amount: 278
2*100 1*50 1*20 0*10 1*5 1*2 1*1
ah i should have thought about using a loop :D
Topic archived. No new replies allowed.