Calculating the dollars right in the Money change program - C++ [closed]

纵饮孤独 提交于 2019-12-24 07:59:38

问题


Please take a look at the below code

#include <QtCore/QCoreApplication>
#include <iostream>

int main(int argc, char *argv[])
{
    using namespace std;

    double purchaseAmount;
    double paidAmount;
    float balance;

     int change, quarters, dimes, nickels, pennies, tenDollar, fiveDollar; // declare variables

    cout << "Enter Total purchased amount" << endl;
    cin >> purchaseAmount;

    cout << "Enter Total paid amount" << endl;
    cin >> paidAmount;

    balance = paidAmount - purchaseAmount ;

     tenDollar = balance / 10; // calculate the number of Ten Dollars
     change =   tenDollar  % 10  ; // calculate the change needed
     change = balance * 100;
     quarters = change / 25; // calculate the number of quarters
     change = change % 25; // calculate remaining change needed
     dimes = change / 10; // calculate the number of dimes
     change = change % 10; // calculate remaining change needed
     nickels = change / 5; // calculate the number of nickels
     pennies = change % 5; // calculate pennies

     cout << "\nQuarters: " << 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
     cout <<" Ten dollar: " << tenDollar << endl; // display # of Ten dollar
     //cout <<" Five dollar: " << fiveDollar << endl; // display # of Ten dollar

     return (0);

 }

What I am trying to do here, calculate the change left in ten dollars,quarters, dimes, nickels and pennies. And for example when I run the program this way -

Enter Total purchased amount
9.75
Enter Total paid amount
20

Quarters: 4
 Dimes: 0
 Nickels: 0
 Pennies: 0
 Ten dollar: 1

Which is wrong. That being said, the above output is wrong. Rather it should be

Enter Total purchased amount
9.75
Enter Total paid amount
20

Quarters: 1
 Dimes: 0
 Nickels: 0
 Pennies: 0
 Ten dollar: 1

So what am I doing wrong here?

Thanks


回答1:


As has been repeatedly said, the mistake was in the painfully repeated code. Consider the following:

int currencyCount(int& pennies, int penniesInDenomination) {
  const int count = penniesInBase / penniesInDenomination;
  pennies = pennies % penniesInDenomination;

  return count;
}

This can be used for each and every denomination -- repeatedly and in one line. This works by having the function get two values: the new balance and the count for that denomination. This sorta cheats by taking the balance by reference and, as a "side effect" of calling this apparently stand alone function, it decreases the balance according to the number of denominations returned. Obviously, you would want to document this.

...
const int numberOfQuarters = currencyCount(balance, 25);
const int numberOfDimes    = currencyCount(balance, 10);
...

You could also put the currency information (such as the name and the number of pennies it represents) in a vector and loop over it performing the same thing:

typedef std::pair<std::string, int> Currency; 
typedef std::vector<Currency> Currencies;
typedef Currencies::const_iterator CIter;

...

for(CIter iter = currencies.begin(); iter != currencies.end(); ++iter) {
  const int quantity = currencyCount(balance, iter->second);
  std::cout << iter->first << ": " << quantity << std::endl;
}

In this manner, you avoid repeated code and the mistakes it involves.




回答2:


Without changing much of your code, you could get your desired answer by replacing

change = balance * 100;

with

change = ((balance) - floor(balance)) * 100;

However, put more thought into your solution, and I guarantee you'll get more points than a simple work-around. Also, don't you dare break conventions, please place your using namespace std outside of main.

Like this:

#include <iostream>
using namespace std; // Place it here! Not inside main.

int main() 
{
 return 0;
}

Note: I said desired answer.



来源:https://stackoverflow.com/questions/12441488/calculating-the-dollars-right-in-the-money-change-program-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!