View Single Post
Old 10-26-2004, 10:13 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
Here, I was having some sheer fun :)
Code:
#include <iostream>
#include <string>
#include <climits>

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

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

//Additional helper. Can *not* be removed. Needed by while-loop.
void reset_istream();

//-- Define a dollar, quarter, dime and nickel in terms of pennies.
static const unsigned PENNIES_IN_DOLLAR = 100;
static const unsigned PENNIES_IN_QUQARTERS = 25;
static const unsigned PENNIES_IN_DIMES = 10;
static const unsigned PENNIES_IN_NICKELS = 5;

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

int main(int argc, char *argv[])
{
   unsigned dollars, quarters, nickels, dimes, pennies;
   
   double total_in_dollars(0);
   cout<<"Enter amount to process (format: dd.cc): ";
   while( !(cin>>total_in_dollars) || total_in_dollars < 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 to process (format: dd.cc): ";
      reset_istream();
   }
   
   //Lets prevent casts by converting the amount to cents only.
   unsigned total_in_cents;
   //Note on precision: "total_in_cents = (int)total_in_dollars * 100" is inaccurate!
   total_in_dollars *= 100;
   total_in_cents = (unsigned)total_in_dollars;

   //Start calculating: offering two approaches (methods).
   
#ifdef MODULUS_METHOD
   //METHOD TYPE: modulus calcualtion. Fast (more or less).

   dollars = total_in_cents / PENNIES_IN_DOLLAR;
   total_in_cents %=  PENNIES_IN_DOLLAR;
   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.

   dollars = total_in_cents / PENNIES_IN_DOLLAR;
   total_in_cents -= dollars * PENNIES_IN_DOLLAR;
   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<< "\t\tAmount expressed in terms of change:" << endl;
   cout<< "\t\t\t" << dollars << " 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);
}
__________________
Valmont is offline   Reply With Quote