calculating change with the basic commands

This is a discussion on calculating change with the basic commands within the C++ Programming forums, part of the General Programming Boards category; I need some small help hopefully. I thought i could figure this out on my own but obviously since i ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    calculating change with the basic commands

    I need some small help hopefully. I thought i could figure this out on my own but obviously since i am posting to this board for the first time, well you know.


    I am trying to get this program to calculate change tendered by breaking it down by dollars, half-dollars, quarters, dimes, nickels, and pennies. Reliaze this is just the beginning of the class so not allowed to use loops, modulus, etc.. this is why i am stuck.

    Code:
    #include <iostream>// cin, cout, <<, >>
    using namespace std;
    
    int main()
    {
        cout << "Enter the amount of purchase" <<"\n";
    	double purchaseAmt;
    	cin >> purchaseAmt;
    	cout << "Enter your payment amount given" <<"\n";
    	double paymentAmt;
    	cin >> paymentAmt;
    	double changeAmt = paymentAmt - purchaseAmt;
    	cout << "\nYour change back is:   " << changeAmt << "\n";
    	double halfDollar = changeAmt / .50;
    	double quarters = changeAmt / .25;
        double dimes = changeAmt / .10;
        double nickels = changeAmt / .5;
        double pennies = changeAmt;
    	cout << "\nhalfDollar back:  " << halfDollar << "\n";
    	cout << "\nQuarters back:  " << quarters << "\n";
    	cout << "\nDimes back:   " << dimes << "\n";  
    	cout << "\nNickels back:  " << nickels << "\n";
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    God why are all you guys never giving info ? What is wrong ? What is your question ? What have you tried ? What's your compiler ? Give us some meat to work on, dude.

    Basically what you have to do is divide the payment amount by the highest unit and go down to cents. So let's say you divide by 1.00 dollar then by .25 dollar, then .10 dollar, then .05 dollar and finally .01 dollar. Each time you divide, you have to substract the answer of the division to the payment amount.

    Code:
    int dollars = (int)paymentAmt; // Same as 'paymentAmt / 1.00'
    paymentAmt -= dollars;
    int quarters = paymentAmt / 0.25;
    paymentAmt -= quarters * 0.25;
    // ...
    Or something like this. Haven't compiled.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reference parameters and calculating change
    By Cstudent2121 in forum C Programming
    Replies: 6
    Last Post: 11-04-2005, 03:19 PM
  2. Executing system commands cross platform
    By markucd in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2005, 08:56 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM
  5. Replies: 2
    Last Post: 09-04-2001, 03:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21