Simplify nested loops with functions?

I have to re-engineer, re-write, simplify and modularize my program’s source code using functions. I'm confused as to where to start. I get the general concept but I have so many loops I don't know how to move them all into functions.
It's a big program and I need to make it smaller and more efficient.


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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  #include <iostream>

using namespace std;

int main(int argc, char** argv)
{

//Initiating variables
    double dblPurchaseAmount, dblCashTendered, dblChangeDue;

    int intTwenty = 0, intTen = 0, intFive = 0, intOne = 0, intQuarters = 0,
intDimes = 0, intNickels = 0, intPennies = 0;

//Creating title
    cout << "********The Change Machine********\n";

//Prompting user for input
    cout << "Enter the amount of the purchase: ";
    cin >> dblPurchaseAmount;

    cout << "Enter cash tendered: ";
    cin >> dblCashTendered;
    cout << "\n";

//Initating variables
    double dblTotalChange;

    dblTotalChange = dblCashTendered - dblPurchaseAmount;
    dblChangeDue = dblTotalChange;

//Initating if statements
    if (dblTotalChange >= 20)
    {
        do
            {
                dblTotalChange = dblTotalChange - 20;
                intTwenty = intTwenty +1 ;
            }
        while (dblTotalChange > 20);
    }

    if (dblTotalChange >= 10)
    {
        do
            {
                dblTotalChange = dblTotalChange - 10;
                intTen = intTen +1;
            }
        while (dblTotalChange > 10);
    }

    if (dblTotalChange >= 5)
    {
        do
            {
            dblTotalChange = dblTotalChange - 5;
            intFive = intFive + 1;
            }
        while (dblTotalChange > 5);
    }

    if (dblTotalChange >= 1)
    {
        do
        {
            dblTotalChange = dblTotalChange - 1;
            intOne = intOne + 1;
        }
        while (dblTotalChange > 1);
    }
    if (dblTotalChange >= 0.25)
    {
        do
        {
            dblTotalChange = dblTotalChange - 0.25;
            intQuarters = intQuarters + 1;
        }
        while (dblTotalChange > 0.25);
    }
    if (dblTotalChange >= 0.10)
    {
        do
        {
            dblTotalChange = dblTotalChange - 0.10;
            intDimes = intDimes + 1;
        }
        while (dblTotalChange > 0.10);
    }
    if (dblTotalChange >= 0.05)
    {
        do
        {
            dblTotalChange = dblTotalChange - 0.05;
            intNickels = intNickels + 1;
        }
        while (dblTotalChange > 0.05);
    }

    if (dblTotalChange >= 0.01);
    {
        do
        {
            dblTotalChange = dblTotalChange - 0.01;
            intPennies = intPennies + 1;
        }
        while (dblTotalChange >= 0.01);
    }

//output section
//showing user results
    cout << "Amount of purchase: ";
    cout << dblPurchaseAmount;
    cout << "\n";

    cout << "Cash tendered: ";
    cout << dblCashTendered;
    cout << "\n";

    cout << "Change owed: ";
    cout << dblChangeDue;
    cout << "\n";

//initating if statements

    cout << "\n";

    if (intTwenty > 0)
    {
        cout <<"The amount of twenties owed is: ";
        cout << intTwenty;
        cout <<"\n";
    }

    if (intTen > 0)
    {
        cout << "The amount of tens owed is: ";
        cout << intTen;
        cout << "\n";
    }

    if (intFive > 0)
    {
        cout << "The amount of fives owed is: ";
        cout << intFive;
        cout << "\n";
    }

    if (intOne > 0)
    {
        cout << "The amount of ones owed is: ";
        cout << intOne;
        cout << "\n";
    }

    if (intQuarters > 0)
    {
        cout << "The amount of quarters owed is: ";
        cout << intQuarters;
        cout << "\n";
    }

    if (intDimes > 0)
    {
        cout << "The amount of dimes owed is: ";
        cout << intDimes;
        cout << "\n";
    }

    if (intNickels > 0)
    {
        cout << "The amount of nickels owed is: ";
        cout << intNickels;
        cout << "\n";
    }

    if (intPennies > 0)
    {
        cout << "The amount of pennies owed is: ";
        cout << intPennies;
        cout << "\n";
    }
    return 0;
}


Any help or guidance is much appreciated!! Thank you!
Start with moving a single if statement to its own function.

Start with
1
2
3
4
5
6
7
8
9
if (dblTotalChange >= 20)
    {
        do
            {
                dblTotalChange = dblTotalChange - 20;
                intTwenty = intTwenty +1 ;
            }
        while (dblTotalChange > 20);
    }


for example.
You can simply cut and paste this code into its own function. You then need to think about what arguments it should take so you can write the function header. Then call the function from where you cut this code from (writing in the required arguments)
Like this? I don't understand how to simplify it more :S


1
2
3
4
5
6
7
8
9
10
11
12
int bills(double dblTotalChange, int intTwenty)
{
    if (dblTotalChange >= 20)
    {
        do
            {
                dblTotalChange = dblTotalChange - 20;
                intTwenty = intTwenty +1 ;
            }
        while (dblTotalChange > 20);
    }
}
Almost, you have the right idea there's just a few missing details. First issue is you've given a return type of int but aren't returning anything. Have a read through http://en.cppreference.com/w/cpp/language/return (it's okay if you don't understand it all at once)
Second is intTwenty is being passed by value http://www.cplusplus.com/forum/general/7990/
So the value intTwenty inside the main function will never change.
So you could have bills accept intTwenty as a reference or pointer, OR bills could simply not take intTwenty but instead return the value.
Last edited on
Okay so I got this far

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>

using namespace std;

double dblPurchaseAmount, dblCashTendered, dblChangeDue, dblTotalChange;

void getInput();
void doTheMath(double, double);
void getOutput(double, double, double);



int main(int argc, char** argv)
{
    getInput();
    getOutput(dblPurchaseAmount, dblCashTendered, dblChangeDue);
    doTheMath(dblPurchaseAmount, dblCashTendered);

    return 0;
}

void getInput()
{

    //Creating title
    cout << "********The Change Machine********\n";

    //Prompting user for input
    cout << "Enter the amount of the purchase: ";
    cin >> dblPurchaseAmount;

    cout << "Enter cash tendered: ";
    cin >> dblCashTendered;
    cout << "\n";
}

void getOutput(double dblAmount, double dblCash, double dblChange)
{
    //output section
    //showing user results
    cout << "Amount of purchase: ";
    cout << dblPurchaseAmount;
    cout << "\n";

    cout << "Cash tendered: ";
    cout << dblCashTendered;
    cout << "\n";

    cout << "Change owed: ";
    cout << dblChangeDue;
    cout << "\n";

}

void doTheMath(double dblAmount, double dblCash)
{
    
//Initiating variables
int intTwenty = 0, intTen = 0, intFive = 0, intOne = 0, intQuarters = 0, intDimes = 0, intNickels = 0, intPennies = 0;


    dblTotalChange = dblCashTendered - dblPurchaseAmount;
    dblChangeDue = dblTotalChange;

//Initating if statements
    if (dblTotalChange >= 20)
    {
        do
            {
                dblTotalChange = dblTotalChange - 20;
                intTwenty = intTwenty +1 ;
            }
        while (dblTotalChange > 20);
    }

    if (dblTotalChange >= 10)
    {
        do
            {
                dblTotalChange = dblTotalChange - 10;
                intTen = intTen +1;
            }
        while (dblTotalChange > 10);
    }

    if (dblTotalChange >= 5)
    {
        do
            {
            dblTotalChange = dblTotalChange - 5;
            intFive = intFive + 1;
            }
        while (dblTotalChange > 5);
    }

    if (dblTotalChange >= 1)
    {
        do
        {
            dblTotalChange = dblTotalChange - 1;
            intOne = intOne + 1;
        }
        while (dblTotalChange > 1);
    }
    if (dblTotalChange >= 0.25)
    {
        do
        {
            dblTotalChange = dblTotalChange - 0.25;
            intQuarters = intQuarters + 1;
        }
        while (dblTotalChange > 0.25);
    }
    if (dblTotalChange >= 0.10)
    {
        do
        {
            dblTotalChange = dblTotalChange - 0.10;
            intDimes = intDimes + 1;
        }
        while (dblTotalChange > 0.10);
    }
    if (dblTotalChange >= 0.05)
    {
        do
        {
            dblTotalChange = dblTotalChange - 0.05;
            intNickels = intNickels + 1;
        }
        while (dblTotalChange > 0.05);
    }

    if (dblTotalChange >= 0.01);
    {
        do
        {
            dblTotalChange = dblTotalChange - 0.01;
            intPennies = intPennies + 1;
        }
        while (dblTotalChange >= 0.01);
    }


//initating if statements

    cout << "\n";

    if (intTwenty > 0)
    {
        cout <<"The amount of twenties owed is: ";
        cout << intTwenty;
        cout <<"\n";
    }

    if (intTen > 0)
    {
        cout << "The amount of tens owed is: ";
        cout << intTen;
        cout << "\n";
    }

    if (intFive > 0)
    {
        cout << "The amount of fives owed is: ";
        cout << intFive;
        cout << "\n";
    }

    if (intOne > 0)
    {
        cout << "The amount of ones owed is: ";
        cout << intOne;
        cout << "\n";
    }

    if (intQuarters > 0)
    {
        cout << "The amount of quarters owed is: ";
        cout << intQuarters;
        cout << "\n";
    }

    if (intDimes > 0)
    {
        cout << "The amount of dimes owed is: ";
        cout << intDimes;
        cout << "\n";
    }

    if (intNickels > 0)
    {
        cout << "The amount of nickels owed is: ";
        cout << intNickels;
        cout << "\n";
    }

    if (intPennies > 0)
    {
        cout << "The amount of pennies owed is: ";
        cout << intPennies;
        cout << "\n";
    }

}


But when I run it, the area that is supposed to print out the change due it just always comes back as 0.
1
2
3
    cout << "Change owed: ";
    cout << dblChangeDue;
    cout << "\n";


You haven't set the variable dblChangeDue to the correct amount at the time you try to print this. It's assigned in the function you call after this.
So how would I set the variable?
Topic archived. No new replies allowed.