Function not recalculating

Below i have posted my project i have recently been assigned for a c++ class at my school. I am supposed to be able to correctly make change for a customer after they give me an amount given in pennies. example is $58.67 would be posted as 5867. My function calculates the correct change the first time but i am supposed to be able to go back and be able to add twenties, tens, fives, etc. if the user needs to. The program correctly compiles but the math isn't correct. I cannot get the function to correctly give me the new change within the switch, it just gives me the original amount. Help needed! Thanks!

// Written By Tim Zumsteg
// Project 05- Chapter 5
// Due March 1, 2013
// C++ for Engineers

#include <iostream>
#include <cmath>
using namespace std;

void make_change(int&, int&, int&, int&, int&, int&, int&, int&, int&);
int twenties=2000, tens=1000, fives=500, ones=100, new_twenties, new_tens, new_fives, new_ones, new_quarters, new_dimes, new_nickels, new_pennies;
int quarters=25, dimes=10, nickels=5, pennies=1, add_twenties, add_tens, add_fives, add_ones, add_quarters, add_dimes, add_nickels, add_pennies;
int total, initial_change;
char command;

int main()
{
void make_change(int&, int&, int&, int&, int&, int&, int&, int&, int&);

int twenties, tens, fives, ones, quarters, dimes, nickels, pennies;

cout<< "\n\n\nWelcome to our store! Please enter the initial amount of change your customer has given you in pennies. ";
cout<< "For example $58.67 would be entered in as 5867.\n";
cin>> initial_change;

cout<< "You have entered in a total of " <<initial_change <<".\n";
cout<< "You need to give the customer the following change:\n\n";

make_change (initial_change, twenties, tens, fives, ones, quarters, dimes, nickels, pennies);

cout<< "\n\n|--------------MENU--------------|\n";
cout<< "| [1] Add Twenties |\n";
cout<< "| [2] Add Tens |\n";
cout<< "| [3] Add Fives |\n";
cout<< "| [4] Add Ones |\n";
cout<< "| [5] Add Quarters |\n";
cout<< "| [6] Add Dimes |\n";
cout<< "| [7] Add Nickels |\n";
cout<< "| [8] Add Pennies |\n";
cout<< "| [9] Refigure Change |\n";
cout<< "| [0] Exit Program |\n";
cout<< "|--------------------------------|\n";

cout<< "\nPlease enter the command you would like to do next. You can add money";
cout<< " or change the amount your customer has given you. You can even exit the program.\n";

cin>> command;

switch (command)
{
void make_change (int&, int&, int&, int&, int&, int&, int&, int&, int&);
int add_twenties, add_tens, add_fives, add_ones, add_quarters, add_dimes, add_nickels, add_pennies;
int new_twenties, new_tens, new_fives, new_ones, new_quarters, new_dimes, new_nickels, new_pennies;
case '1':
cout<< "\n\nEnter amount of twenties you'd like to add.\n";
cin>> add_twenties;
cout<< "You now need to give the customer the current change:\n";

new_twenties= twenties+add_twenties;

make_change (initial_change, new_twenties, tens, fives, ones, quarters, dimes, nickels, pennies);

break;
case '2':
cout<< "\n\nEnter amount of tens you'd like to add.\n";
cin>> add_tens;
cout<< "You now need to give the customer the current change:\n";

new_tens= tens+add_tens;

make_change (initial_change, twenties, new_tens, fives, ones, quarters, dimes, nickels, pennies);

break;
case '3':
cout<< "\n\nEnter amount of fives you'd like to add.\n";
cin>> add_fives;
cout<< "You now need to give the customer the current change:\n";

new_fives= fives+add_fives;

make_change (initial_change, twenties, tens, new_fives, ones, quarters, dimes, nickels, pennies);

break;
case '4':
cout<< "\n\nEnter amount of ones you'd like to add.\n";
cin>> add_ones;
cout<< "You now need to give the customer the current change:\n";

new_ones=ones+add_ones;

make_change (initial_change, twenties, tens, fives, new_ones, quarters, dimes, nickels, pennies);

break;
case '5':
cout<< "\n\nEnter amount of quarters you'd like to add.\n";
cin>> add_quarters;
cout<< "You now need to give the customer the current change:\n";

new_quarters= quarters+add_quarters;

make_change (initial_change, twenties, tens, fives, ones, new_quarters, dimes, nickels, pennies);

break;
case '6':
cout<< "\n\nEnter amount of dimes you'd like to add.\n";
cin>> add_dimes;
cout<< "You now need to give the customer the current change:\n";

new_dimes=dimes+add_dimes;

make_change (initial_change, twenties, tens, fives, ones, quarters, new_dimes, nickels, pennies);

break;
case '7':
cout<< "\n\nEnter amount of nickels you'd like to add.\n";
cin>> add_nickels;
cout<< "You now need to give the customer the current change:\n";

new_nickels=nickels+add_nickels;

make_change (initial_change, twenties, tens, fives, ones, quarters, dimes, new_nickels, pennies);

break;
case '8':
cout<< "\n\nEnter amount of pennies you'd like to add.\n";
cin>> add_pennies;
cout<< "You now need to give the customer the current change:\n";

new_pennies=pennies+add_pennies;

make_change (initial_change, twenties, tens, fives, ones, quarters, dimes, nickels, new_pennies);

break;
case '9':
int new_change;
cout<< "Please enter the amount of change your customer has given you\n";
cin>> new_change;

cout<< "You need to give the customer the current change of:\n\n";

make_change (new_change, twenties, tens, fives, ones, quarters, dimes, nickels, pennies);

break;
case 0:
break;
}

return 0;
}

void make_change (int& initial_change, int& twenties, int& tens, int& fives, int& ones,
int& quarters, int& dimes, int& nickels, int& pennies)

{

twenties= (initial_change/2000);
tens= (initial_change-(twenties*2000))/1000;
fives= (initial_change-(twenties*2000)-(tens*1000))/500;
ones= (initial_change-(twenties*2000)-(tens*1000)-(fives*500))/100;
quarters= (initial_change-(twenties*2000)-(tens*1000)-(fives*500)-(ones*100))/25;
dimes= (initial_change-(twenties*2000)-(tens*1000)-(fives*500)-(ones*100)-(quarters*25))/10;
nickels= (initial_change-(twenties*2000)-(tens*1000)-(fives*500)-(ones*100)-(quarters*25)-(dimes*10))/5;
pennies= (initial_change-(twenties*2000)-(tens*1000)-(fives*500)-(ones*100)-(quarters*25)-(dimes*10)-(nickels*5))/1;

cout<< "Twenties: " << twenties << "\n";
cout<< "Tens: " << tens << "\n";
cout<< "Fives: " << fives << "\n";
cout<< "Ones: " << ones << "\n";
cout<< "\n\nQuarters: " << quarters << "\n";
cout<< "Dimes: " << dimes << "\n";
cout<< "Nickels: " << nickels << "\n";
cout<< "Pennies: " << pennies << "\n";

}

You could put the code for printing the change in a separate function.

As it stands now the only way to change the amount of tens, for instance, in make_change(...) is to alter the initial_change parameter but this may give you extra twenties instead of tens.

If you print the change separately you can just print new_tens instead of tens.

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
// Written By Tim Zumsteg
// Project 05- Chapter 5
// Due March 1, 2013
// C++ for Engineers

#include <iostream>
#include <cmath>
using namespace std;

void make_change(int&, int&, int&, int&, int&, int&, int&, int&, int&);
void print_change( int&, int&, int&, int&, int&, int&, int&, int&);/// new function

int twenties=2000, tens=1000, fives=500, ones=100, new_twenties, new_tens, new_fives, new_ones, new_quarters, new_dimes, new_nickels, new_pennies;
int quarters=25, dimes=10, nickels=5, pennies=1, add_twenties, add_tens, add_fives, add_ones, add_quarters, add_dimes, add_nickels, add_pennies;
int total, initial_change;
char command;

int main()
{
//void make_change(int&, int&, int&, int&, int&, int&, int&, int&, int&); //// already declared your function above

//int twenties, tens, fives, ones, quarters, dimes, nickels, pennies; ////use the global variables above

cout<< "\n\n\nWelcome to our store! Please enter the initial amount of change your customer has given you in pennies. ";
cout<< "For example $58.67 would be entered in as 5867.\n";
cin>> initial_change;

cout<< "You have entered in a total of " <<initial_change <<".\n";
cout<< "You need to give the customer the following change:\n\n";

make_change (initial_change, twenties, tens, fives, ones, quarters, dimes, nickels, pennies);
print_change(twenties, tens, fives, ones, quarters, dimes, nickels, pennies); //// new function

cout<< "\n\n|--------------MENU--------------|\n";
cout<< "| [1] Add Twenties |\n";
cout<< "| [2] Add Tens |\n";
cout<< "| [3] Add Fives |\n";
cout<< "| [4] Add Ones |\n";
cout<< "| [5] Add Quarters |\n";
cout<< "| [6] Add Dimes |\n";
cout<< "| [7] Add Nickels |\n";
cout<< "| [8] Add Pennies |\n";
cout<< "| [9] Refigure Change |\n";
cout<< "| [0] Exit Program |\n";
cout<< "|--------------------------------|\n";

cout<< "\nPlease enter the command you would like to do next. You can add money";
cout<< " or change the amount your customer has given you. You can even exit the program.\n";

cin>> command;

switch (command)
{
//void make_change (int&, int&, int&, int&, int&, int&, int&, int&, int&); //// function already declared above
//int add_twenties, add_tens, add_fives, add_ones, add_quarters, add_dimes, add_nickels, add_pennies; //// will use the global variables
//int new_twenties, new_tens, new_fives, new_ones, new_quarters, new_dimes, new_nickels, new_pennies; //// will use the global variables
case '1':
cout<< "\n\nEnter amount of twenties you'd like to add.\n";
cin>> add_twenties;
cout<< "You now need to give the customer the current change:\n";

new_twenties= twenties+add_twenties;

//make_change (initial_change, new_twenties, tens, fives, ones, quarters, dimes, nickels, pennies); //// don't need to recalculate since global variables
print_change(new_twenties, tens, fives, ones, quarters, dimes, nickels, pennies); //// new function. note use new_twenties

break;

....

case '9':
int new_change;
cout<< "Please enter the amount of change your customer has given you\n";
cin>> new_change;

cout<< "You need to give the customer the current change of:\n\n";

make_change (new_change, twenties, tens, fives, ones, quarters, dimes, nickels, pennies); //// do need to recalculate change
print_change(twenties, tens, fives, ones, quarters, dimes, nickels, pennies); /// new function

break;
case 0:
break;
}

return 0;
}

void make_change (int& initial_change, int& twenties, int& tens, int& fives, int& ones,
int& quarters, int& dimes, int& nickels, int& pennies)

{

twenties= (initial_change/2000);
tens= (initial_change-(twenties*2000))/1000;
fives= (initial_change-(twenties*2000)-(tens*1000))/500;
ones= (initial_change-(twenties*2000)-(tens*1000)-(fives*500))/100;
quarters= (initial_change-(twenties*2000)-(tens*1000)-(fives*500)-(ones*100))/25;
dimes= (initial_change-(twenties*2000)-(tens*1000)-(fives*500)-(ones*100)-(quarters*25))/10;
nickels= (initial_change-(twenties*2000)-(tens*1000)-(fives*500)-(ones*100)-(quarters*25)-(dimes*10))/5;
pennies= (initial_change-(twenties*2000)-(tens*1000)-(fives*500)-(ones*100)-(quarters*25)-(dimes*10)-(nickels*5))/1;

}

void print_change(int& twenties, int& tens, int& fives, int& ones,
int& quarters, int& dimes, int& nickels, int& pennies)
{

cout<< "Twenties: " << twenties << "\n";
cout<< "Tens: " << tens << "\n";
cout<< "Fives: " << fives << "\n";
cout<< "Ones: " << ones << "\n";
cout<< "\n\nQuarters: " << quarters << "\n";
cout<< "Dimes: " << dimes << "\n";
cout<< "Nickels: " << nickels << "\n";
cout<< "Pennies: " << pennies << "\n";

}
Last edited on
Topic archived. No new replies allowed.