Good programming practices

Hi all! First post on the forum. I've been reading some of the articles which I find extremely helpful. My question deals with good programming practices, but first a very brief background: I'm currently reading "Practical C++ Programming" by Steve Oualline (who also happens to be a Syracuse grad, like myself). I'm just reading a chapter and doing all the practice problems. So, I'll post the problem I'm working on, my code that solves the problem, and below that, my question.

Problem: write a program that computes the number of quarters, dimes, nickels, and pennies needed, given an amount less than $1.00 (it tells you how to make change).

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <cstring>
#include <cassert>
#include <math.h>

int main()
{
	using namespace std;

	// Declare variables to use
	float total_money(0);              //stores the user entered value for amount of money
	int   total_money_int(0);          //used to store the integer value for money
	int   num_quarters(0);             //used to store the calculated number of quarters
	int   num_dimes(0);                //used to store the calculated number of dimes
	int   num_nickels(0);              //used to store the calculated number of nickels
	int   num_pennies(0);              //used to store the calculated number of pennies
	int   leftover_after_quarter(0);   //temp storage for money leftover after quarters are subtracted
	int   leftover_after_dimes(0);     //temp storage for money leftover after quarters and dimes subtracted
	int   leftover_after_nickels(0);   //temp storage for money leftover after quarters, dimes, nickels subtracted
	char  another_value('y');          //stores flag that determines if the user would like to do another calculation

	cout << "This program takes a monetary value greater than 0.00 and less than 1.00 and" << endl;
	cout << "outputs the number of each type of coin required to make change for that amount." <<endl;
	cout << "\n";

	do {
	
		// **************************************************
		// *    Read in value for money from user      *
		// **************************************************
		cout << "Enter a value less than $1.00 and greater than $0.00: ";
		cin >> total_money;
		cout << endl;      //using an endl instead of '\n' flushes the buffer
		
		if (total_money >= 1.00) {
			cout << "Value is too high. Enter a value less than $1.00." << endl;
			continue;
		}
		if (total_money <= 0.00) {
			cout << "Value is too low. Enter a value greater than $0.00." << endl;
			continue;
		}

		// **************************************************
		// *    Compute the number of each type of coin needed      *
		// **************************************************
		total_money_int = int (total_money * 100);  //cast float input to integer for upcomiong integer division
	
		num_quarters = total_money_int/25;       //integer division, computes # of quarters
		leftover_after_quarter = total_money_int % 25;  

		num_dimes = leftover_after_quarter/10;   //integer division, computes # of dimes
		leftover_after_dimes = leftover_after_quarter % 10;

		num_nickels = leftover_after_dimes/5;    //integer division, computes # of nickels
		leftover_after_nickels = leftover_after_dimes % 5;

		num_pennies = leftover_after_nickels/1;  //integer division, computes # of pennies

		// **************************************************
		// *     If check for output statements. Only display note for non zero amount of coins       *
		// **************************************************
		cout << "-------------------------------------------------------------------" << endl;
		if (num_quarters > 0) cout << "The number of quarters is: " << num_quarters << " quarters." << endl;
		if (num_dimes    > 0) cout << "The number of dimes is   : " << num_dimes    << " dimes." << endl;
		if (num_nickels  > 0) cout << "The number of nickels is : " << num_nickels  << " nickels." << endl;
		if (num_pennies  > 0) cout << "The number of pennies is : " << num_pennies  << " pennies." << endl;
		if (num_quarters == 0 && num_dimes == 0 && num_nickels == 0 && num_pennies == 0) {
			cout << "No coins.";
		}
		cout << "-------------------------------------------------------------------" << endl;
		cout << endl;
		cout << "Would you like to perform another calculation? If so, press 'y'. Otherwise," << endl;
		cout << "press any other key to exit." << endl;
		cout << "Another calculation? ";
		cin >> another_value;

	} while (another_value == 'y' || another_value == 'Y');

	return 0;
}


My question(s) : I created the do/while loop and then put in the checks on the user input afterwards. The best way I could make a continue statement work with a do/while loop was to initialize another_value to 'y'. Is this bad programming practice? Intuitively it seems like I should initialize it to 'n' or something that will break out of the loop if things go wrong. Also, I've been including the same files above in every problem, whether I need them or not. Is this bad practice? I'm assuming in a real system including things you don't need eats up resources somehow? Any other glaring flaws with the DESIGN of my solution?

Thanks a bunch!
K.F.
Well to be fair the only library you need for this program is <iostream> there is no need for <string>, <math.h> and the other stuff. I would rather use a while loop for this. here is what I suggest

char answer;

while(answer == 'y')
{

// block

cout <<"Would you like to make another calculation(y/n) ?;
cin >> answer;

}


so whenever the answer is other than yes ('y') the loop wont run. For design purposes you can use else if and else instead of just using plain ifs all over the place.
Last edited on
@jkevin

1. answer needs to be initialised, otherwise the loop might not run;
2. Use equality operator, not assignment.
Topic archived. No new replies allowed.