Please take a look at the below code

#include <QtCore/QCoreApplication>
#include <iostream>

int main(int argc, char *argv[])
{
    using namespace std;

    double purchaseAmount;
    double paidAmount;
    float balance;

     int change, quarters, dimes, nickels, pennies, tenDollar, fiveDollar; // declare variables

    cout << "Enter Total purchased amount" << endl;
    cin >> purchaseAmount;

    cout << "Enter Total paid amount" << endl;
    cin >> paidAmount;

    balance = paidAmount - purchaseAmount ;

     tenDollar = balance / 10; // calculate the number of Ten Dollars
     change =   tenDollar  % 10  ; // calculate the change needed
     change = balance * 100;
     quarters = change / 25; // calculate the number of quarters
     change = change % 25; // calculate remaining change needed
     dimes = change / 10; // calculate the number of dimes
     change = change % 10; // calculate remaining change needed
     nickels = change / 5; // calculate the number of nickels
     pennies = change % 5; // calculate pennies

     cout << "\nQuarters: " << quarters << endl; // display # of quarters
     cout << " Dimes: " << dimes << endl; // display # of dimes
     cout << " Nickels: " << nickels << endl; // display # of nickels
     cout <<" Pennies: " << pennies << endl; // display # of pennies
     cout <<" Ten dollar: " << tenDollar << endl; // display # of Ten dollar
     //cout <<" Five dollar: " << fiveDollar << endl; // display # of Ten dollar

     return (0);

 }

What I am trying to do here, calculate the change left in ten dollars,quarters, dimes, nickels and pennies. And for example when I run the program this way -

Enter Total purchased amount
9.75
Enter Total paid amount
20

Quarters: 4
 Dimes: 0
 Nickels: 0
 Pennies: 0
 Ten dollar: 1

Which is wrong. That being said, the above output is wrong. Rather it should be

Enter Total purchased amount
9.75
Enter Total paid amount
20

Quarters: 1
 Dimes: 0
 Nickels: 0
 Pennies: 0
 Ten dollar: 1

So what am I doing wrong here?

Thanks

Oops, sorry, I posted wrong. @Admins: Please delete this post

Edited 4 Years Ago by myk45

No, I want it to show

 Quarters: 1
 Dimes: 0
 Nickels: 0
 Pennies: 0
 Ten dollar: 1

Since, thats what the change is to be given to the customer.

I hope i make sense

Yeah, sorry about my last post. Anyway, here is the problem:

balance -= 10 * tenDollar; is needed before you do:

change = balance * 100;

Also, why is this needed?

change = tenDollar % 10 ; // calculate the change needed

Hi @myk45,

change = tenDollar % 10 ; // calculate the change needed

Wasnt needed.

balance -= 10 * tenDollar;

Fixed it all.

Thanks so much..:)

Also, why is this needed?

change = tenDollar % 10 ; // calculate the change needed

he probably wanted the remaining decimal values to use for the quarter computation but this wouldn't work since tenDollars is an integer type and he can't use the modulo operator for float type variables

Edited 4 Years Ago by zeroliken

Comments
Agreed :)
This article has been dead for over six months. Start a new discussion instead.