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 10-26-2004, 04:37 PM   #1 (permalink)
eghonda
Registered User
 
Join Date: Oct 2004
Posts: 1
eghonda is on a distinguished road
1st time CPP help

i need to create a program which will break down an amount of money into: dollars, quarters, dimes, nickels, and pennies.

for instance $5.36= 5 dollars, 1 quarter, 1 dime, and 1 penny.

i played around a bit but i reached a point where i was stuck. i ended up with 2 sets of input/output statements. i need only one but once i remove the other the program ceases to function. basically i need "enter the change amount" as the intput and "change" as the input.

here's the portion of code that is troubling me:


int main()
{
float change;
int changeneeded;
int quarters=40, dimes=50, nickels=40, pennies=50;
int numq, numd, numn, nump;
cout <<"Enter the change amount: " ;
cin >>change;

while(change>0)
{
cout << "Enter the change amount: ";
cin >> change;
cout << ((int)(change*100))/100 << " dollar bills ";
changeneeded = ((int)(change*100))%100;

compute_coin(25, numq,changeneeded);
compute_coin(10, numd,changeneeded);
compute_coin(5, numn,changeneeded);
compute_coin(1, nump,changeneeded);

quarters-=numq;
dimes-=numd;
nickels-=numn;
pennies-=nump;



--any help would be greatly appreciated. thanks.
eghonda is offline   Reply With Quote
Old 10-26-2004, 09: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
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
Using IsDate( ) to check for a Time value shs MS Technologies ( ASP, VB, C#, .NET ) 1 09-09-2004 12:03 PM
network time synchronisation in java bossebo Java 1 06-21-2004 05:17 PM
Adjusting date() function for time zones Epsilon PHP 1 02-27-2004 02:55 AM
Assign value to a variable depending on time AOD PHP 10 02-12-2003 03:06 PM


All times are GMT -8. The time now is 07:11 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