Eliminating lines with if statements
Dec 26, 2012 at 10:22pm UTC
This program calculates the least amount of change a person can give. For example if I would type in $26.50 it would print out -
Twenties-1
Fives- 1
Loonies-1
Quarters-2
Also for the bills and coins not used how would I get the to not show instead of the giving me zeros.
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <iostream>
using namespace std;
const int FIFTIES = 5000;
const int TWENTIES = 2000;
const int TENS = 1000;
const int FIVES = 500;
const int TWOS = 200;
const int ONES = 100;
const int QUARTER = 25;
const int DIME = 10;
const int NICKEL = 5;
const int PENNIES_TO_DOLLAR = 100;
int main()
{
int DollarAmt, DecimalAmt;
char DecimalPoint;
int TotalMoneyInPennies;
cout<<"This program will convert a total amount of money into denominations of money to make change." <<endl;
cout<<"Input the total here: " ;
cin>> DollarAmt >> DecimalPoint >> DecimalAmt;
cout<<endl;
TotalMoneyInPennies = DecimalAmt + (DollarAmt * PENNIES_TO_DOLLAR);
cout<<"You input: $" << DollarAmt << DecimalPoint << DecimalAmt<<endl;
cout<<"This is a total of " <<TotalMoneyInPennies <<" pennies." <<endl;
cout<<"Your denominations would be: " <<endl;
cout<< "Fifties: " <<TotalMoneyInPennies/FIFTIES<<endl;
TotalMoneyInPennies = TotalMoneyInPennies % FIFTIES;
cout<< "Twenties: " <<TotalMoneyInPennies/TWENTIES<<endl;
TotalMoneyInPennies = TotalMoneyInPennies % TWENTIES;
cout<< "Tens: " <<TotalMoneyInPennies/TENS<<endl;
TotalMoneyInPennies = TotalMoneyInPennies % TENS;
cout<< "Fives: " <<TotalMoneyInPennies/FIVES<<endl;
TotalMoneyInPennies = TotalMoneyInPennies % FIVES;
cout<< "Toonies: " <<TotalMoneyInPennies/TWOS<<endl;
TotalMoneyInPennies = TotalMoneyInPennies % TWOS;
cout<< "Loonies: " <<TotalMoneyInPennies/ONES<<endl;
TotalMoneyInPennies = TotalMoneyInPennies % ONES;
cout<< "Quarters: " <<TotalMoneyInPennies/QUARTER<<endl;
TotalMoneyInPennies = TotalMoneyInPennies % QUARTER;
cout<< "Dimes: " <<TotalMoneyInPennies/DIME<<endl;
TotalMoneyInPennies = TotalMoneyInPennies % DIME;
cout<< "Nickels: " <<TotalMoneyInPennies/NICKEL<<endl;
TotalMoneyInPennies = TotalMoneyInPennies % NICKEL;
cout<< "Pennies: " <<TotalMoneyInPennies<<endl;
system ("pause" );
}
Last edited on Dec 26, 2012 at 10:28pm UTC
Dec 27, 2012 at 8:03am UTC
First calculate the value it should be, e.g. int fifties = TotalMoneyInPennies/FIFTIES;
. Then check whether fifties <= 0. If not, do the cout statement. Otherwise only calculate the TotalMoneyInPennies.
Topic archived. No new replies allowed.