Thread: I'm Stuck Help
View Single Post
Old 11-01-2004, 10:04 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
As promised, the code below:
Code:
#include <iostream>
#include <string>
#include <climits>
#include <fstream>

using std::cin;
using std::cout;
using std::numeric_limits;
using std::streamsize;
using std::string;
using std::endl;
using std::ifstream;

//Optional function: depends on IDE. Prevents from "console flashing".
//1) Remove if not needed by IDE. 2) Remove for final release.
void wait_for_enter();

//Additional helper.
void reset_istream();

//-- Define the monetary system in terms of pennies.
static const unsigned PENNIES_IN_FIFTY = 5000;
static const unsigned PENNIES_IN_TWENTY = 2000;
static const unsigned PENNIES_IN_TEN = 1000;
static const unsigned PENNIES_IN_FIVE = 500;
static const unsigned PENNIES_IN_DOLLAR = 100;
static const unsigned PENNIES_IN_HALF_DOLLARS = 50;
static const unsigned PENNIES_IN_QUQARTERS = 25;
static const unsigned PENNIES_IN_DIMES = 10;
static const unsigned PENNIES_IN_NICKELS = 5;

//A little cheat: otherwise such as 200.01 - 50.02 differs with 1 cent.
static const double TOLERANCE = 0.00000000001;

//Outcomment ONE of these two preprocessors to enable a calculation method.
#define MODULUS_METHOD
//#define STRAIGTFORWARD_METHOD

int main(int argc, char *argv[])
{
   unsigned fifties, twenties, tens, fives, dollars, halvedollars ,
      quarters, nickels, dimes, pennies;
   double total_change(0), amount_tendered(0), amount_of_sale(0);

   cout<<"Enter amount of sale (format: dd.cc): ";
   while( !(cin>>amount_of_sale) || amount_of_sale < 0.01 )
   {
      cout<<endl;
      cout<<"ERROR! invalid input. Requirements for valid input follows below:"<<endl;
      cout<<"1) Enter only floating point values."<<endl;
      cout<<"2) Floating point value must be > 0."<<endl;
      cout<<"\nTry again please. Enter amount of sale (format: dd.cc): ";
      reset_istream();
   }
   
   cout<<"Enter amount tendered (format: dd.cc): ";
   while( !(cin>>amount_tendered) || amount_tendered < 0.01 || amount_tendered < amount_of_sale )
   {
      cout<<endl;
      cout<<"ERROR! invalid input. Requirements for valid input follows below:"<<endl;
      cout<<"1) Enter only floating point values."<<endl;
      cout<<"2) Floating point value must be > 0."<<endl;
      cout<<"3) Amount tendered must be => amount of sale."<<endl;
      cout<<"\nTry again please. Enter mount tendered (format: dd.cc): ";
      reset_istream();
   }

   total_change =  amount_tendered - amount_of_sale;
   //Lets prevent multiple casts by converting the amount to cents only.
   unsigned total_in_cents;
   total_in_cents = (unsigned)(total_change * 100 + TOLERANCE);

   //Start calculating: offering two approaches (methods).

#ifdef MODULUS_METHOD
   //METHOD TYPE: modulus calcualtion. Fast (more or less).
   cout<<"\n*** using MODULUS_METHOD ***"<<endl;

   fifties = total_in_cents / PENNIES_IN_FIFTY;
   total_in_cents %= PENNIES_IN_FIFTY;

   twenties = total_in_cents / PENNIES_IN_TWENTY;
   total_in_cents %= PENNIES_IN_TWENTY;
   
   tens = total_in_cents / PENNIES_IN_TEN;
   total_in_cents %= PENNIES_IN_TEN;
   
   fives = total_in_cents / PENNIES_IN_FIVE;
   total_in_cents %= PENNIES_IN_FIVE;
   
   dollars = total_in_cents / PENNIES_IN_DOLLAR;
   total_in_cents %=  PENNIES_IN_DOLLAR;
   
   halvedollars = total_in_cents / PENNIES_IN_HALF_DOLLARS;
   total_in_cents %= PENNIES_IN_HALF_DOLLARS;
   
   quarters = total_in_cents / PENNIES_IN_QUQARTERS;
   total_in_cents %=  PENNIES_IN_QUQARTERS;

   dimes = total_in_cents / PENNIES_IN_DIMES;
   total_in_cents %= PENNIES_IN_DIMES;

   nickels = total_in_cents / PENNIES_IN_NICKELS;
   pennies = total_in_cents % PENNIES_IN_NICKELS;
#endif //MODULUS_METHOD

#ifdef STRAIGTFORWARD_METHOD
   //METHOD TYPE: straight forward. Needs additional calculation per type to determine leftover.
   cout<<"\n*** using STRAIGHTFORWARD_METHOD ***"<<endl;

   fifties = total_in_cents / PENNIES_IN_FIFTY;
   total_in_cents -= fifties * PENNIES_IN_FIFTY;
   
   twenties = total_in_cents / PENNIES_IN_TWENTY;
   total_in_cents -= twenties * PENNIES_IN_TWENTY;
   
   tens = total_in_cents / PENNIES_IN_TEN;
   total_in_cents -= tens * PENNIES_IN_TEN;
   
   fives = total_in_cents / PENNIES_IN_FIVE;
   total_in_cents -= fives * PENNIES_IN_FIVE;

   dollars = total_in_cents / PENNIES_IN_DOLLAR;
   total_in_cents -= dollars * PENNIES_IN_DOLLAR;
   
   halvedollars = total_in_cents / PENNIES_IN_HALF_DOLLARS;
   total_in_cents -= halvedollars * PENNIES_IN_HALF_DOLLARS;
   
   quarters = total_in_cents / PENNIES_IN_QUQARTERS;
   total_in_cents -= quarters * PENNIES_IN_QUQARTERS;
   
   dimes = total_in_cents / PENNIES_IN_DIMES;
   total_in_cents -= dimes * PENNIES_IN_DIMES;
   
   nickels = total_in_cents / PENNIES_IN_NICKELS;
   pennies = total_in_cents - nickels * PENNIES_IN_NICKELS;
#endif //STRAIGHTFORWARD_METHOD

   cout<<endl;
   cout<<"Amount of sale: $"<<amount_of_sale<<endl;
   cout<<"Amount tendered: $"<<amount_tendered<<endl;
   cout<<"Your total change $"<<total_change<<endl;
   cout<<endl;
   cout<< "\t\tAmount expressed in terms of change:" << endl;
   cout<< "\t\t\t" << fifties << " fifties" << endl;
   cout<< "\t\t\t" << twenties << " twenties" << endl;
   cout<< "\t\t\t" << tens << " tens" << endl;
   cout<< "\t\t\t" << fives << " fives" << endl;
   cout<< "\t\t\t" << dollars << " dollars" << endl;
   cout<< "\t\t\t" << halvedollars << " half-dollars" << endl;
   cout<< "\t\t\t" << quarters << " quarters" << endl;
   cout<< "\t\t\t" << dimes << " dimes" << endl;
   cout<< "\t\t\t" << nickels << " nickels" << endl;
   cout<< "\t\t\t" << pennies << " pennies" << endl<<endl;

   //These two functions co-operate together. If not needed by IDE, then remove both calls.
   reset_istream();
   wait_for_enter();
   return 0;
}


//---------------------------------------------------

void reset_istream()
{
   if(cin.eof())
   {
      cin.clear();
   }
   else
   {
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
   }
}

//---------------------------------------------------

void wait_for_enter()
{
  cout << "press <enter> to continue...";
  // Reset failstate, just in case.
  cin.clear();
  string line;
  getline( cin, line);
}
__________________

Last edited by Valmont; 11-01-2004 at 11:42 PM.
Valmont is offline   Reply With Quote