Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-01-2004, 03:12 PM   #1 (permalink)
verdell32
Registered User
 
verdell32's Avatar
 
Join Date: Oct 2004
Posts: 35
verdell32 is on a distinguished road
I'm Stuck Help

I am working in Borland C++ 5 and these are the specifications:

Write a program that inputs the amount of a sale and an amount tendered by a customer. The program should then calculate the total amount of change due and display the same. Then the program should calculate the individual bills and coins that would be used to give change back to a customer.

Sample output: Another example:

Amount tendered : $200.00
Amount of sale : $121.62
Your total change: $ 78.38
--------------------------------- -----------------------------------
1 fifty
1 twenty
0 tens
1 five
3 ones
0 half dollars
1 quarter
1 dime
0 nickels
3 pennies

Thanks for all your help.
Here's what I have so far:

Code:
// Program Documentation //////////////////////////////////////////////////////
//
// Project Name  : CHANGE
//
// Source File   : change.cpp
//
// Programmed By :Shang Fields
//
// Last Revision : 11/1/2004
//
// Version Number: 0.1
//
// Program Description ////////////////////////////////////////////////////////
//
// This program the user inputs the amount of a sale and an amount tendered
// by customer. Then the program will calculate the individual bills
// and coins that will be used to give change back to the customer.
///////////////////////////////////////////////////////////////////////////////

// Include Files //////////////////////////////////////////////////////////////
#include <iostream.h>
#include <conio.h>

// Function Prototypes ////////////////////////////////////////////////////////

// Program Mainline ///////////////////////////////////////////////////////////

int main( )
{
   //-- Define a fifty,twenty,tens,five,ones,halfadollar, quarter, dime and nickel in terms of pennies.
   static const unsigned PENNIES_IN_FIFTY = 50;
   static const unsigned PENNIES_IN_TWENTY = 20;
   static const unsigned PENNIES_IN_TENS = 10;
   static const unsigned PENNIES_IN_FIVE = 5;
   static const unsigned PENNIES_IN_ONES = 1;
	static const unsigned PENNIES_IN_HALFDOLLARS = 50;
	static const unsigned PENNIES_IN_QUQARTERS = 25;
	static const unsigned PENNIES_IN_DIMES = 10;
	static const unsigned PENNIES_IN_NICKELS = 5;
   float total_in_cents;
   float amountTendered;
   float amountOfSale;
   float totalChange;

   //Input data
   cout<<"Enter amount tendered:";
   cin>>amountTendered;
   cout<<"Enter amount of sale:";
   cin>>amountOfSale;
   cout>>totalChange;


   //Calculations
   fifty = total_in_cents / PENNIES_IN_FIFTY;
   total_in_cents -= fifty * PENNIES_IN_FIFTY;
   twenty = total_in_cents / PENNIES_IN_TWENTY;
   total_in_cents -= twenty * PENNIES_IN_TWENTY;
   tens = total_in_cents / PENNIES_IN_TENS;
   total_in_cents -= tens * PENNIES_IN_TENS;
   five = total_in_cents / PENNIES_IN_FIVE;
   total_in_cents -= five * PENNIES_IN_FIVE;
   ones = total_in_cents / PENNIES_IN_ONES;
   total_in_cents -= ones * PENNIES_IN_ONES;
   halfdollars = total_in_cents / PENNIES_IN_HALFDOLLARS;
   total_in_cents -= halfdollars * PENNIES_IN_HALFDOLLARS;
   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;
   totalChange = amountTendered - amountOfSale;
   
   cout<<endl;
   cout<< "\t\tAmount expressed in terms of change:" << endl;
   cout<< "\t\t\t" << fifty << " fifty" << endl;
   cout<< "\t\t\t" << twenty << " twenty" << endl;
   cout<< "\t\t\t" << tens << " tens" << endl;
   cout<< "\t\t\t" << five << " five" << endl;
   cout<< "\t\t\t" << ones << " ones" << endl;
   cout<< "\t\t\t" << halfdollars << "halfdollars" << 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;
   cout<<endl;

   getch();
   return 0;
}//endmn
:p

Last edited by Valmont; 11-01-2004 at 08:16 PM.
verdell32 is offline   Reply With Quote
Old 11-01-2004, 09: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 10:42 PM.
Valmont is offline   Reply With Quote
Old 11-02-2004, 02:06 PM   #3 (permalink)
verdell32
Registered User
 
verdell32's Avatar
 
Join Date: Oct 2004
Posts: 35
verdell32 is on a distinguished road
Thank you

I thank you for your help.
verdell32 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
New to MySQL - (need help please) bobredcar Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 11 08-23-2004 07:47 AM


All times are GMT -8. The time now is 10:45 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting