I am working on a C++ app the compiles, and works. (Sort of) The purpose of the app is the user inputs an amount of money such as $200.61. The app tells you how many dollars, quarters, dimes, nickels, and pennies to return. Similar to 200 dollars, 2 quarters, 1 dime, 0 nickels, 1 penny.
Here is my code:
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
/////////////////////////////////////////////////
struct change //Structure
{
float total;
int dollars;
int quarters;
int dimes;
int nickels;
int pennies;
};
//////////////////////////////////////////////////////
change makeChange (float money);
int _tmain(int argc, _TCHAR* argv[])
{
float totalMoney;
change changeAmount;
cout << "Enter the amount of money 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 << " nickles 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 remainingMoney1 = money - makeChange.quarters;
makeChange.dimes = static_cast <int> (remainingMoney1*10);
float remainingMoney2 = money - makeChange.dimes;
makeChange.nickels = static_cast <int> (remainingMoney2*20);
float remainingMoney3 = money - makeChange.nickels;
makeChange.pennies = static_cast <int> (remainingMoney3*100);
float remainingMoney4 = money - makeChange.pennies;
return(makeChange);
Yes it is ugly when you copy and paste it. Anyhow, if you compile and run you see that I get an output for $200.61 that looks like this.
200 Dollars. 2 quarters, 1986 dimes, -35707 nickels, 3590760 pennies.
The point of the assignment is to use the function and the structure. So those need to stay. I think the problem is coming from within the function.
I'm guess the problem has something to do with the "money" variable. I have tried a few things and they all seem to make the compiler very angry.
Any tips or hints?
Last edited by kerowo; 10-11-2009 at 09:17 PM.