I am trying to write a program that can figure change. Something that might be used in a fast food setting. The Employee is simply prompted to enter the amount to be returned to the costumer and the program would tell how many dollars, quarters, dimes, ect. to be returned.
Here is what I have.

#include "stdafx.h"
#include <iostream>
using namespace std;

struct change
{
	int dollars;
	int quarters;
	int dimes;
	int nickels;
	int pennies;
};

change makeChange(float money);

int main()
{

	float totalMoney;
	change changeAmount;
	cout << "Enter the total to be returned to the customer: ";
	cin >> totalMoney;
	changeAmount = makeChange(totalMoney);
	cout << "Give the customer " 
		<< changeAmount.dollars << " dollars, "
		<< changeAmount.quarters << " quarters, "
		<< changeAmount.dimes << " dimes, "
		<< changeAmount.nickels << " nickels, and "
		<< changeAmount.pennies << " pennies.\n";

change makeChange(float money)
{

	change makeChange;
	makeChange.dollars = static_cast<int>(money);
	float remainingMoney = money - makeChange.dollars;
	makeChange.quarters = static_cast<int>(remainingMoney * 4);
	float remainingMoney = money - makeChange.quarters;
	makeChange.dimes = static_cast<int>(remainingMoney * 10);
	float remainingMoney = money - makeChange.dimes;
	makeChange.nickels = static_cast<int>(remainingMoney * 20);
	float remainingMoney = money - makeChange.nickels;
	makeChange.pennies = static_cast<int>(remainingMoney * 100);
	return(makeChange);
}

	return 0;
}

I know one of the problems is with remainingMoney that appears in lines 36-42
Another thing is I am getting an error that reads 'makeChange' : local function definitions are illegal

Any help with getting this program properly working is much appreciated.

You cannot define a function inside another function. So, your main( ) must be closed return 0 } before you start the definition of makeChange( )

Your technique of multiplying by number of coins in a dollar is unusual, but seems sound. In removing that coin's worth from the remaining total, however, you must subtract number of coins * coin value

Okay, I got the function definition inside another function fixed.
But i do not fully understand this

Your technique of multiplying by number of coins in a dollar is unusual, but seems sound. In removing that coin's worth from the remaining total, however, you must subtract number of coins * coin value

And I am getting a couple errors
error C2374: 'remainingMoney' : redefinition; multiple initialization
as well as
see declaration of 'remainingMoney'
these codes are related with lines 36+

#include "stdafx.h"
#include <iostream>
using namespace std;

struct change
{
	int dollars;
	int quarters;
	int dimes;
	int nickels;
	int pennies;
};

change makeChange(float money);

int main()
{
	float totalMoney;
	change changeAmount;
	cout << "Enter the total to be returned to the customer: ";
	cin >> totalMoney;
	changeAmount = makeChange(totalMoney);
	cout << "Give the customer " 
		<< changeAmount.dollars << " dollars, "
		<< changeAmount.quarters << " quarters, "
		<< changeAmount.dimes << " dimes, "
		<< changeAmount.nickels << " nickels, and "
		<< changeAmount.pennies << " pennies.\n";
	return 0;
}

change makeChange(float money)
{
	change makeChange;
	makeChange.dollars = static_cast<int>(money);
	float remainingMoney = money - makeChange.dollars;
	makeChange.quarters = static_cast<int>(remainingMoney * 4);
	float remainingMoney = money - makeChange.quarters;
	makeChange.dimes = static_cast<int>(remainingMoney * 10);
	float remainingMoney = money - makeChange.dimes;
	makeChange.nickels = static_cast<int>(remainingMoney * 20);
	float remainingMoney = money - makeChange.nickels;
	makeChange.pennies = static_cast<int>(remainingMoney * 100);
	return(makeChange);
}

That error message is telling you what's wrong.

Why do you put the data type in front of the variable every time?

That error message is telling you what's wrong.

Why do you put the data type in front of the variable every time?

because I don't know this language. actually i don't even know what a data type is so i couldn't really tell you

In the makeChange function remove the data type 'float' from the remainingMoney variable you only need to put float in front of the variable once.

Okay first off I would like to thank you guys for the help. The program actually compiles now. Only thing is the dimes nickles, and pennies don't come out accurately. The dollars and quarters work though, what I don't get is I used the same logic for everything but only the dollars and quarters work.

Looking over your code the part that stands out is

makeChange.pennies = static_cast<int>(remainingMoney * 100);

You dont have a remainingMoney = ....... like in all the other change denominations. Try adding that to the code.

I tried that with no luck. I am going to attempt to upload a screen shot of the problem, with my current code.

#include "stdafx.h"
#include <iostream>
using namespace std;

struct change
{
	int dollars;
	int quarters;
	int dimes;
	int nickels;
	int pennies;
};

change makeChange(float money);

int main()
{
	float totalMoney;
	change changeAmount;
	cout << "Enter the total to be returned to the customer: ";
	cin >> totalMoney;
	changeAmount = makeChange(totalMoney);
	cout << "Give the customer " 
		<< changeAmount.dollars << " dollars, "
		<< changeAmount.quarters << " quarters, "
		<< changeAmount.dimes << " dimes, "
		<< changeAmount.nickels << " nickels, and "
		<< changeAmount.pennies << " pennies.\n";
	return 0;
}

change makeChange(float money)

{
	float remainingMoney;
	change makeChange;
	makeChange.dollars = static_cast<int>(money);
	remainingMoney = money - makeChange.dollars;
	makeChange.quarters = static_cast<int>(remainingMoney * 4);
	remainingMoney = money - makeChange.quarters;
	makeChange.dimes = static_cast<int>(remainingMoney * 10);
	remainingMoney = money - makeChange.dimes;
	makeChange.nickels = static_cast<int>(remainingMoney * 20);
	remainingMoney = money - makeChange.nickels;
	makeChange.pennies = static_cast<int>(remainingMoney * 100);
	remainingMoney = money - makeChange.pennies;
	return(makeChange);
}
Attachments Problem.jpg 20.88 KB

Okay first off I would like to thank you guys for the help. The program actually compiles now. Only thing is the dimes nickles, and pennies don't come out accurately. The dollars and quarters work though, what I don't get is I used the same logic for everything but only the dollars and quarters work.

You're welcome.

Now go back and read my first reply again, relating to the calculation of remaininMoney.

You have $1.78. Your code correctly stores 1 dollar, and subtracts 1.00 from the total, leaving 0.78. You then calculate number of quarters by taking the integer portion of the value ( 0.78 * 4 ), which is 3. You then subtract the number of quarters ( 3 ) from the remaining total ( 0.78 ) giving you....-2.22. Hmmm, that's not right.

Knowing that you're removing 3 quarters from the total, what amount should be subtracted? How should your program create that amount?

So it should look something more like this...

change makeChange(float money)


{
	float remainingMoney;
	change makeChange;
	makeChange.dollars = static_cast<int>(money);
	remainingMoney = money - makeChange.dollars;
	makeChange.quarters = static_cast<int>(remainingMoney * 4);
	remainingMoney = money - (quarters * .25);
	makeChange.dimes = static_cast<int>(remainingMoney * 10);
	remainingMoney = money - (dimes * .10);
	makeChange.nickels = static_cast<int>(remainingMoney * 20);
	remainingMoney = money - (nickels * .05);
	makeChange.pennies = static_cast<int>(remainingMoney * 100);
	remainingMoney = money - (pennies * .01);
	return(makeChange);
}

now the number of the coins is multiplied by the value of the coin and that is subtracted from the amount remaining. But it won't compile errors are saying that quarters, dimes, nickles, and pennies are all undeclared identifiers, but arent they declared at the very beginning of my code as int's?

makeChange.quarters = static_cast<int>(remainingMoney * 4);
remainingMoney = money - ( quarters * .25);

Okay now it is compiling again, and I think the negative numbers showing up is fixed but the dimes and down from there is still not coming out right.

change makeChange(float money)
{
	float remainingMoney;
	change makeChange;
	makeChange.dollars = static_cast<int>(money);
	remainingMoney = money - makeChange.dollars;
	makeChange.quarters = static_cast<int>(remainingMoney * 4);
	remainingMoney = money - ( makeChange.quarters * .25);
	makeChange.dimes = static_cast<int>(remainingMoney * 10);
	remainingMoney = money - ( makeChange.dimes * .10);
	makeChange.nickels = static_cast<int>(remainingMoney * 20);
	remainingMoney = money - ( makeChange.nickels * .05);
	makeChange.pennies = static_cast<int>(remainingMoney * 100);
	remainingMoney = money - ( makeChange.pennies * .01);
	return(makeChange);
}

Well, for one thing, you are casting between ints and floats everytime you calculate everything which is a bad idea as it leads to a loss of precision which can snowball.

Edited 7 Years Ago by jbennet: n/a

This article has been dead for over six months. Start a new discussion instead.