Below is the code I have, and the output is below that. It all seems right, but I don't want it to print out the 0's if they appear in the output. How do I do that?

``````#include <iostream>
#include <iomanip>

using namespace std;

int main() {

double moneyOwed = 0.0;
double moneyPaid = 0.0;
double change = 0.0;
int change1 = 0;
int dollars = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;

cout << "Customer owes: \$";
cin >> moneyOwed;

cout << "Customer pays: \$";
cin >> moneyPaid;

cout << fixed << setprecision(2);
//calculations
change = moneyPaid - moneyOwed;
change1 = change * 100;
dollars = change1 / 100;
quarters = change1 % 100 / 25;
dimes = change1 % 100 % 25 / 10;
nickels = change1 % 100 % 25 % 10 / 5;
pennies = change1 % 100 % 25 % 10 % 5;

//change
cout << "Change due: \$" << change << endl;
cout << "Dollars: " << dollars << endl;
cout << "Quarters: " << quarters << endl;
cout << "Dimes: " << dimes << endl;
cout << "Nickles: " << nickels << endl;
cout << "Pennies: " << pennies << endl;

cin >> change;
return 0;
}

Customer owes: \$1.30
Customer owes: \$2.00
Change due: \$0.70
Dollars: 0
Quarters: 2
Dimes: 2
Nickles: 0
Pennies: 0``````

## All 6 Replies

Use an `if` statement as in

``````if (dollars > 0) {
cout << "Dollars: " << dollars << endl;
}``````

In this case I think it would be acceptable to use a single line format as

``if (dollars > 0) cout << "Dollars: " << dollars << endl;``

While that is a proper fix, for the code you have, I think that, with a bit of refactoring, you can accomplish what you want and make your code more efficient.

First, instead of using a bunch of literal constants, sometimes referred to as Magic Numbers, defining some constants to represent those values, makes your code that much easier to understand:

``````const double MAX_UNDER_DOLLAR = .99;
const int DOLLAR = 100;
const int QUARTER = 25;
const int DIME = 10;
const int NICKEL = 5;``````

Second use the conditional statements to decide whether to calculate for that particular denomination and print out the result:

``````    if (change1 > QUARTER)
{
quarters = change1 / QUARTER;
cout << "Quarters: " << quarters << '\n';
change1 -= quarters * QUARTER;
}``````

When we put this together it could look something like this:

``````#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const double MAX_UNDER_DOLLAR = .99;
const int DOLLAR = 100;
const int QUARTER = 25;
const int DIME = 10;
const int NICKEL = 5;

int main()
{
double moneyOwed = 0.0;
double moneyPaid = 0.0;
cout << "Customer owes: \$";
cin >> moneyOwed;
cout << "Customer pays: \$";
cin >> moneyPaid;
cout << fixed << setprecision(2);
//calculations
double change = moneyPaid - moneyOwed;
double change1 = change;
cout << "Change due: \$" << change << '\n';
int dollars = 0;
if (change1 > MAX_UNDER_DOLLAR)
{
dollars = (int) (change1);
cout << "Dollars: " << dollars << '\n';
change1 -= dollars;
}
change1 *= DOLLAR;
int quarters = 0;
if (change1 > QUARTER)
{
quarters = change1 / QUARTER;
cout << "Quarters: " << quarters << '\n';
change1 -= quarters * QUARTER;
}
int dimes = 0;
if (change1 > DIME)
{
dimes = change1 / DIME;
cout << "Dimes: " << dimes << '\n';
change1 -= dimes * DIME;
}
int nickels = 0;
if (change1 > NICKEL)
{
nickels = change1 / NICKEL;
cout << "Nickels: " << nickels << '\n';
change1 -= nickels * NICKEL;
}
int pennies = 0;
if (change1 > 0)
{
pennies = change1;
cout << "Pennies: " << pennies << '\n';
change1 = 0;
}
cin.ignore(1);
cin.get();
}``````

The output based on your sample:

Customer owes: \$1.3
Customer pays: \$2
Change due: \$0.70
Quarters: 2
Dimes: 2

Some side notes:

Declare your variables as close to where they will be used as possible. The compiler doesn't care and on a large project this will help you find the declaration if you need to change it.

Only use `endl` if you need the stream to be flushed. Use `'\n'` instead for most cases.

To pause execution I think you'll find :

``````    cin.ignore(1);
cin.get();``````

to be more clearer for what you intend. Just throwing a random `cin` statement in there can be confusing.

In this case I think that using constants is unnecessary. It adds nothing to the code simply because the actual values are so obvious that the constants add only to the length of the overall code.

``quarters = (change1 % 100) / 25;``

is pretty self-explanatory and making a const for `25` is tantamount to adding comments like `add one to x`. If you feel otherwise I would suggest

``quarters = (change1 % 100) / 25;    \\ 25 = cents in a quarter``

I don't feel like coding this up in c/c++ (I'm quite rusty) but a vbScript version is

``````set arg = Wscript.Arguments

if arg.Count <> 2 then
wscript.echo "change cost cash"
wscript.quit
end if

cost = Int(cdbl(arg(0)) * 100)
cash = Int(cdbl(arg(1)) * 100)

set denom = CreateObject("Scripting.Dictionary")
denom(10000) = Array("hundred","hundreds")
denom( 5000) = Array("fifty","fifties")
denom( 2000) = Array("twenty","twenties")
denom( 1000) = Array("ten","tens")
denom(  500) = Array("five","fives")
denom(  100) = Array("one","ones")
denom(   25) = Array("quarter","quarters")
denom(   10) = Array("dime","dimes")
denom(    5) = Array("nickel","nickels")
denom(    1) = Array("penny","pennies")

balance = cash - cost

for each denomination in denom.keys

num = balance \ denomination

if num > 0 then
if num = 1 then
wscript.echo num,denom(denomination)(0)
else
wscript.echo num,denom(denomination)(1)
end if
balance = balance - num * denomination
end if

next``````

Slightly complicated with a dictionary because I wanted to handle both singular and plural amounts.

Having some extra length in the code doesn't hurt anything as long as it's for efficiency and/or readability. The compiler will optimize it anyway.

Using constants is more about following good and accepted practices and principles to avoid a lot of hassle on larger projects. Using these principles on simple things and getting them ingrained as habits will reap huge dividends in later projects.

A map in c++ would work but ,unless the scope of the project is to be expanded, the overhead is way more than what's needed for such a simple project.

I follow good and accepted practices and principles, just not blindly. But you are correct in that it's better to develop the habit early. One can relax it later when one is older and (possibly) wiser ^_^

I think it has something to do with format specifiers and something else.

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.