Telling program not to output value when...

Hi, I am writing a program to output a dollar value in 20's bills, 10's bills, 5's bills, and singles.

So if I had something like $54, it would display 2 x 20's bills, 1 x 10's bills, and 4 x singles bills.

I'm having problems with getting the program to NOT display 0 x 5's bills.

Is there a way to write a while loop which checks to see if any of the values is 0 and if the value is 0, makes sure that IT IS NOT DISPLAYED and only displays the values that are not 0
Last edited on
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

void makeChange(double money, int &twenties, int &tens, int &fives, int &singles, int &quarters, int &dimes, int &nickels, int
&pennies){
int y
int z
y = int(money);
twenties = y / 20;
tens = (y % (20*twenties)) / 10;
fives = (y % ((20*twenties) + (10*tens))) / 5;
singles = (y % ((20*twenties) + (10*tens) + (5*fives))) / 1;
z = money - y;
quarters = z / 0.25;
dimes = (z % (0.25*quarters)) / 0.10;
nickels = (z % ((0.25*quarters) + (0.10*dimes))) / 0.05;
pennies = (z % ((0.25*quarters) + (0.10*dimes) + (0.05*nickels))) / 0.01;
return;
}

void printChange(ofstream &ofile, double money, int twenties, int tens, int fives, int singles, int quarters, int dimes, int
nickels, int pennies){
ofile << "The change for" << money << endl;

if (twenties + tens + fives + singles == 0)
ofile << "In notes is:" << "Nothing" << endl;

else if (twenties != 0 && tens != 0 && fives != 0 && singles != 0)
ofile << "In notes is:" << twenties << " x $20 Dollars, " << tens << " x $10 Dollars, " << fives << " x $5 Dollars, " <<
singles << " x $1 Dollars." << endl;

else if (twenties != 0 && tens != 0 && fives != 0 && singles == 0)
ofile << "In notes is:" << twenties << " x $20 Dollars, " << tens << " x $10 Dollars, " << fives << " x $5 Dollars." <<
endl;

else if (twenties != 0 && tens != 0 && fives == 0 && singles == 0)
ofile << "In notes is:" << twenties << " x $20 Dollars, " << tens << " x $10 Dollars." << endl;

else if (twenties != 0 && tens == 0 && fives == 0 && singles == 0)
ofile << "In notes is:" << twenties << " x $20 Dollars." << endl;

else if (twenties == 0 && tens != 0 && fives != 0 && singles != 0)
ofile << "In notes is:" << tens << " x $10 Dollars, " << fives << " x $5 Dollars, " << singles << " x $1 Dollars." << endl;

else if (twenties == 0 && tens == 0 && fives != 0 && singles != 0)
ofile << "In notes is:" << fives << " x $5 Dollars, " << singles << " x $1 Dollars." << endl;

else if (twenties == 0 && tens == 0 && fives == 0 && singles != 0)
ofile << "In notes is:" << singles << " x $1 Dollars." << endl;

else if (twenties == 0 && tens != 0 && fives == 0 && singles == 0)
ofile << "In notes is:" << tens << " x $10 Dollars,." << endl;

else if (twenties == 0 && tens == 0 && fives != 0 && singles == 0)
ofile << "In notes is:" << fives << " x $5 Dollars." << endl;

else if (twenties == 0 && tens != 0 && fives != 0 && singles == 0)
ofile << "In notes is:" << tens << " x $10 Dollars, " << fives << " x $5 Dollars." << endl;

else if (twenties == 0 && tens != 0 && fives == 0 && singles != 0)
ofile << "In notes is:" << tens << " x $10 Dollars, " << singles << " x $1 Dollars." << endl;

else if (twenties != 0 && tens == 0 && fives != 0 && singles == 0)
ofile << "In notes is:" << twenties << " x $20 Dollars, " << fives << " x $5 Dollars." << endl;

else if (twenties != 0 && tens == 0 && fives != 0 && singles != 0)
ofile << "In notes is:" << twenties << " x $20 Dollars, " << fives << " x $5 Dollars, " << singles << " x $1 Dollars." <<
endl;

else if (twenties != 0 && tens != 0 && fives == 0 && singles != 0)
ofile << "In notes is:" << twenties << " x $20 Dollars, " << tens << " x $10 Dollars, " << singles << " x $1 Dollars." <<
endl;
// this is the code I have without the while loop. As you can see, I had to write A LOT of if statements that took into account every possibility. Is there a way to express thin in a smaller while loop?
Wouldnt this be a lot simpler to do. Just add "__ x $(whatever amount)" in the output.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main () 
{
	int amount, twenty, ten, five, one;
	
	cout << "Enter your amount:  "; //allows user to enter amount
	cin >> amount;
	
	twenty = amount / 20;
	amount = amount % 20;
	ten = amount / 10;
	amount = amount % 10;
	five = amount / 5;
	one = amount / one;
	
	cout << "Total bills are: " << endl;
	cout << twenty << " twenty(s)." <<endl;
	cout << ten << "  ten(s)" <<endl;
	cout << five << "  five(s)" <<endl;
	cout << one << "  one(s)" <<endl;
	
	system("pause");
	return 0;
	
}
Topic archived. No new replies allowed.