Making Change

Well I'm supposed write a program that makes change using his kind of outline. The instructions are at "http://haruspex.net/csc1253/as4-5.pdf" assignment 5. Can someone help me out with this. Its not giving the correct change. See output VV
*Update on what I have*
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
/*
  Name:
  Date: 14/10/10 15:04
  Description: Making Change
*/

#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;
ifstream infile;
ofstream outfile;

void make_change(float cost, float payment, float& change,
                 int& twentiesInTill, int& tensInTill, int& fivesInTill,
                 int& dollarsInTill, int& quartersInTill, int& dimesInTill,
                 int& nickelsInTill, int& penniesInTill, int& twentiesInChange,
                 int& tensInChange, int& fivesInChange, int& dollarsInChange,
                 int& quartersInChange, int& dimesInChange,
                 int& nickelsInChange, int& penniesInChange) 
{
       change = (payment - cost);
       int changeInPennies = static_cast<int>(change * 100.0 + 0.001);
       if(changeInPennies > 2000){
          for(twentiesInChange = 0; twentiesInTill >= twentiesInChange; twentiesInChange++){
             twentiesInTill = (twentiesInTill - 1);
             changeInPennies = (changeInPennies - 2000);}
             }
       else if(changeInPennies > 1000){
          for(tensInChange = 0; tensInTill >= tensInChange; tensInChange++){
             tensInTill = (tensInTill - 1);
             changeInPennies = (changeInPennies - 1000);}
             }
       else if(changeInPennies > 500){
          for(fivesInChange = 0; fivesInTill >= fivesInChange; fivesInChange++){
             fivesInTill = (fivesInTill - 1);
             changeInPennies = (changeInPennies - 500);}
             }
       else if(changeInPennies > 100){
          for(dollarsInChange = 0; dollarsInTill >= dollarsInChange; dollarsInChange++){
             dollarsInTill = (dollarsInTill - 1);
             changeInPennies = (changeInPennies - 100);}
             }
       else if(changeInPennies > 25){
          for(quartersInChange = 0; quartersInTill >= quartersInChange; quartersInChange++){
             quartersInTill = (quartersInTill - 1);
             changeInPennies = (changeInPennies - 25);}
             }
       else if(changeInPennies > 10){
          for(dimesInChange = 0; dimesInTill >= dimesInChange; dimesInChange++){
             dimesInTill = (dimesInTill - 1);
             changeInPennies = (changeInPennies - 10);}
             }
       else if(changeInPennies > 5){
          for(nickelsInChange = 0; nickelsInTill >= nickelsInChange; nickelsInChange++){
             nickelsInTill = (nickelsInTill - 1);
             changeInPennies = (changeInPennies - 5);}
             }
       else if(changeInPennies > 1){
          for(penniesInChange = 0; penniesInTill >= penniesInChange; penniesInChange++){
             penniesInTill = (penniesInTill - 1);
             changeInPennies = (changeInPennies - 1);}
             }

       outfile << "Cost =   "<< cost << "\nPayment =   "<< payment << "\n";
       outfile << "Change =   "<< change << ", consisting of:\n" << twentiesInChange << " twenties ";
       outfile << tensInChange << " tens " << fivesInChange << " fives ";
       outfile << dollarsInChange << " dollars " << quartersInChange << " quarters ";
       outfile << dimesInChange << " dimes " << nickelsInChange << " nickels ";
       outfile << penniesInChange << " pennies\nTill Contents:\n";
       outfile << twentiesInTill << " twenties " << tensInTill << " tens ";
       outfile << fivesInTill << " fives " << dollarsInTill << " dollars " << quartersInTill << " quarters ";
       outfile << dimesInTill << " dimes " << nickelsInTill << " nickels " << penniesInTill << " pennies\n\n";
       
}
int main(){
    infile.open("transactions.dat");
       if (infile.fail()) {
          cerr << "Can't open transactions.dat for input.\n";
          return 1;}
    outfile.open("change.out");
       if (outfile.fail()){
          cerr << "Can't open change.out for output.\n";
          return 2;}
    float cost, payment, change;
    int twentiesInTill, tensInTill, fivesInTill, dollarsInTill, quartersInTill, dimesInTill, nickelsInTill, penniesInTill;
    int twentiesInChange, tensInChange, fivesInChange, dollarsInChange, quartersInChange, dimesInChange, nickelsInChange, penniesInChange;
    infile >> twentiesInTill >> tensInTill >> fivesInTill >> dollarsInTill >> quartersInTill >> dimesInTill >> nickelsInTill >> penniesInTill;
    outfile << "Initial Till Contents:\n" << twentiesInTill << " twenties " << tensInTill << " tens ";
    outfile <<  fivesInTill << " fives " << dollarsInTill << " dollars " << quartersInTill << " quarters ";
    outfile <<  dimesInTill << " dimes " << nickelsInTill << " nickels " << penniesInTill << " pennies\n\n";
    while(infile >> cost >> payment){
       make_change(cost, payment, change, twentiesInTill, tensInTill, fivesInTill,
                  dollarsInTill, quartersInTill, dimesInTill,nickelsInTill, 
                  penniesInTill, twentiesInChange, tensInChange, fivesInChange, 
                  dollarsInChange, quartersInChange, dimesInChange,
                  nickelsInChange, penniesInChange);}
    infile.close();
    outfile.close();
    return 0;
}

It outputs
Initial Till Contents:
5 twenties 5 tens 10 fives 20 dollars 40 quarters 50 dimes 40 nickels 50 pennies

Cost =   2.83
Payment =   3
Change =   0.17, consisting of:
2001898709 twenties -1076866825 tens -2 fives 2001770714 dollars 2001838330 quarters 26 dimes 3477112 nickels 2686776 pennies
Till Contents:
5 twenties 5 tens 10 fives 20 dollars 40 quarters 24 dimes 40 nickels 50 pennies

Cost =   2.34
Payment =   20
Change =   17.66, consisting of:
2001898709 twenties 3 tens -2 fives 2001770714 dollars 2001838330 quarters 26 dimes 3477112 nickels 2686776 pennies
Till Contents:
5 twenties 2 tens 10 fives 20 dollars 40 quarters 24 dimes 40 nickels 50 pennies

Cost =   2.53
Payment =   100
Change =   97.47, consisting of:
3 twenties 3 tens -2 fives 2001770714 dollars 2001838330 quarters 26 dimes 3477112 nickels 2686776 pennies
Till Contents:
2 twenties 2 tens 10 fives 20 dollars 40 quarters 24 dimes 40 nickels 50 pennies

Cost =   2.3
Payment =   100
Change =   97.7, consisting of:
2 twenties 3 tens -2 fives 2001770714 dollars 2001838330 quarters 26 dimes 3477112 nickels 2686776 pennies
Till Contents:
0 twenties 2 tens 10 fives 20 dollars 40 quarters 24 dimes 40 nickels 50 pennies

Cost =   0.99
Payment =   1
Change =   0.00999999, consisting of:
2 twenties 3 tens -2 fives 2001770714 dollars 2001838330 quarters 26 dimes 3477112 nickels 2686776 pennies
Till Contents:
0 twenties 2 tens 10 fives 20 dollars 40 quarters 24 dimes 40 nickels 50 pennies

Cost =   3.21
Payment =   3.21
Change =   0, consisting of:
2 twenties 3 tens -2 fives 2001770714 dollars 2001838330 quarters 26 dimes 3477112 nickels 2686776 pennies
Till Contents:
0 twenties 2 tens 10 fives 20 dollars 40 quarters 24 dimes 40 nickels 50 pennies

Cost =   0.98
Payment =   20
Change =   19.02, consisting of:
2 twenties 2 tens -2 fives 2001770714 dollars 2001838330 quarters 26 dimes 3477112 nickels 2686776 pennies
Till Contents:
0 twenties 0 tens 10 fives 20 dollars 40 quarters 24 dimes 40 nickels 50 pennies

Cost =   9.32
Payment =   10
Change =   0.68, consisting of:
2 twenties 2 tens -2 fives 2001770714 dollars 21 quarters 26 dimes 3477112 nickels 2686776 pennies
Till Contents:
0 twenties 0 tens 10 fives 20 dollars 19 quarters 24 dimes 40 nickels 50 pennies

Cost =   5.78
Payment =   10
Change =   4.22, consisting of:
2 twenties 2 tens -2 fives 11 dollars 21 quarters 26 dimes 3477112 nickels 2686776 pennies
Till Contents:
0 twenties 0 tens 10 fives 9 dollars 19 quarters 24 dimes 40 nickels 50 pennies


Last edited on
Ive updated it to where I'm currently at. Could anyone help me out?
Topic archived. No new replies allowed.