vending machine function weird output

Hey all so I got a vending machine program and I'm having trouble with this last function to calculate and give back the change. I am passing by referance the number of quarters, dime and nickels in the machine, the amount of each inputted and the change, then calculating the number of each coin to give back based on the amount of change. The problem I'm having is that it correctly gives back the quarters and dimes but the nickels gives back all in the machine and I'm not sure why since it is the same for the other coins. anyone have any ideas why I'm getting this problem?

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
  void returnChange(int& quarterCount, int& dimeCount, int& nickelCount,
					  int& returnQuarter, int& returnDime, int& returnNickel, double& change)
{
if (change < 0)
	{
		cout << "Enter more money please";
	
	}

		if (change > (quarterCount * QUARTER + dimeCount * DIME + nickelCount * NICKEL))
		{
		cout << "there is not enough change, heres your money back";
		}

		else
		{
		while (change >= .25 && quarterCount > 0)
		{
			returnQuarter++;
			quarterCount--;
			change = change -.25;
		}
		while (change >= .10 && dimeCount > 0)
		{
		returnDime++;
		dimeCount--;
		change = change - .10;

		}
		while (change >= .05 && nickelCount > 0)
		{
		returnNickel++;
		nickelCount--;
		change = change - .05;
		}
		}
		

	cout << "you get back " << returnQuarter << " quarters " <<
	returnDime << " dimes " << nickelCount << " nickels";
Last edited on
1
2
cout << "you get back " << returnQuarter << " quarters " <<
	returnDime << " dimes " << nickelCount << " nickels";


Looks like you're keeping track of how many coins to return in the returnQuarter, returnDime and returnNickel variables? You're printing out nickelCount which seems to be tracking the number of nickels left in the register?
Topic archived. No new replies allowed.