Its an exact change program but the output is not right. I don't know whats wrong in it. Can somebody please help me with program.

#include <iostream>
using namespace std;

int main ()

{

    int twenties, tens, fives, singles, quarters, dimes, nickles, pennies;
    double purchase, pay, rempay=0;
    cout<< "please enter the purchase amount ";
    cin>> purchase;
    cout<< " please enter the payment amount ";
    cin>> pay;
    rempay = pay - purchase;
    twenties = int(rempay/20); 
    rempay= rempay - 20;
    tens= int(rempay/10);
    rempay = rempay - 10;
    fives= int(rempay/5);
    rempay= rempay - 5;
    singles = int(rempay / 1);
    rempay = rempay - 1;

    quarters = int(rempay / .25);

    rempay = rempay - .25;
    dimes = int(rempay / .10);
    rempay = rempay - .10;
    nickles = int(rempay / .05);
    rempay= rempay - .05;
    pennies= int(rempay / .01);



    cout<< "twenties - " << twenties << '\n';
    cout << "tens - " << tens << '\n';
    cout <<" fives - " << fives << '\n';
    cout << "singles - " << singles << '\n';


    cout << "Quarters - " << quarters << '\n';
    cout << "Dimes - " << dimes << '\n';
    cout << "Nickels - " << nickles << '\n';
    cout << "Pennies - " << pennies << '\n';





    return 0;
}
#include <iostream>
using namespace std;

int main ()

{

int twenties, tens, fives, singles, quarters, dimes, nickles, pennies;
double purchase, pay, rempay=0;
cout<< "please enter the purchase amount ";
cin>> purchase;
cout<< " please enter the payment amount ";
cin>> pay;
rempay = pay - purchase;
twenties = int(rempay/20);
rempay= rempay - 20;
tens= int(rempay/10);
rempay = rempay - 10;
fives= int(rempay/5);
rempay= rempay - 5;
singles = int(rempay / 1);
rempay = rempay - 1;

quarters = int(rempay / .25);

rempay = rempay - .25;
dimes = int(rempay / .10);
rempay = rempay - .10;
nickles = int(rempay / .05);
rempay= rempay - .05;
pennies= int(rempay / .01);



cout<< "twenties - " << twenties << '\n';
cout << "tens - " << tens << '\n';
cout <<" fives - " << fives << '\n';
cout << "singles - " << singles << '\n';


cout << "Quarters - " << quarters << '\n';
cout << "Dimes - " << dimes << '\n';
cout << "Nickels - " << nickles << '\n';
cout << "Pennies - " << pennies << '\n';



cin.ignore();
cin.get();
return 0;
}

Added cin.ignore() and cin.get() just before main finishes execution.

Even so, this is just a temporary problem. You can't rely on this if the user give improper input.

I'll pm you with what I have. Really sorry, thought you couldn't get an output going with the way the code was--

Note: You may want to consider using the modulus operator to carry the remainder of the amount one set of payment couldn't fully pay off naturally...

For example...

Purchase is 300, Pay is 410

110%20 will be 5 remainder 10.

Doing int division is only half it, you'll have to calculate the remainder to be divided by the next form of payment and mod that result as well.

you should consider using a modulus operator for several reasons.

In you code, when you divide by rempay, unless the result is a value greater than 1, the value you get for rempay will not give you the desired output. You have no conditions in your code to check whether the value of rempay before deducting the appropriate amount from it. to illustrate my point, consider

purchase = 27
pay = 30

when you divide by 20 and takeaway 20, rempay = 3,
then you devide by ten, tens is 0.3, and then your code deducts 10 from rempay without any set conditions, so rempay now becomes -7, and then folowing through the code , your output will give you the wrong values.

i suggest you use an if statement to check conditions whereby before you deduct the amounts, it checks to see how much, if any, you should deduct.

eg: from my ilustration

rempay= rempay - 20;
if(tens > 1)
    rempay -= 10;
fives= int(rempay/5);

otherwise you should consider using the modulus function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.