Change is always zero?

No matter what I put in for cost of item change comes back as zero?
Is there a way to see what value is being passed from first set of code to the second? I have tried multiple things but none seem to work.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	double userNumber, cost, taxOwed,taxRate, totalOwed;
	int roundedNumber;
	taxRate = .075;

	cout << "Please enter the cost of the item purchasing: ";
	cin >> userNumber; // input the amount of cost

	cost = userNumber / 100;
	taxOwed = cost * taxRate;
	
	cout << "The cost of the item you entered is: " << cost << endl;
	cout << "The amount of taxed owed on item is: " << taxOwed << endl;
	totalOwed = cost + taxOwed;
	std::cout << setprecision(2);
	roundedNumber = static_cast<int>(totalOwed + .5);
	cout << "The total amount you owe is: " << totalOwed;

	int change, quarters, dimes, nickels, pennies;
	change = totalOwed;

		quarters = change / 25;
		change = change % 25;
		dimes = change / 10;
		change = change % 10;
		nickels = change / 5;
		pennies = change % 5;
		
		cout << "\n\n Quarters: " << quarters << endl; // display # of quarters
		cout << " Dimes: " << dimes << endl; // display # of dimes
		cout << " Nickels: " << nickels << endl; // display # of nickels
		cout << " Pennies: " << pennies << endl; // display # of pennies

		return 0;
}


Please enter the cost of the item purchasing: 61
The cost of the item you entered is: 0.61
The amount of taxed owed on item is: 0.04575
The total amount you owe is: 0.66

Quarters: 0
Dimes: 0
Nickels: 0
Pennies: 0


With above cost should get back
Quarter 1
Dime 1
Nickel 0
Pennies 4

Thanks for any suggestions!!

Last edited on
change = 100 - totalOwed *100 + 0.5;
Last edited on
that worked lastchance thanks. Is there a way to test the value that was being passed to change?

.... Nevermind I figured out how to see that was an easy question just didn't think about it.

Not sure why your code works though. I tried

1
2
3
4
change = totalOwed
change = 100-totalOwed
change = 100-totalOwed*100 // this one was close but sometimes is a penny off


what is the +.5 doing? is it doing the same as early in my code by rounding the value?
Last edited on
The +0.5 is ensuring rounding to the nearest integer once you assign a double to an int. A simple cast - or just a straight assignment - would truncate down. e.g. 33.999 would truncate to a not-very-close 33.

I'm guessing that your change is from 100. You never state it.

Oh, and I don't know much about American coinage - I'm from the other side of the pond.
Yes it was from 100 you were correct. Thanks for your assistance.
Topic archived. No new replies allowed.