Assistance writing to a file and removing zeros

Sep 11, 2019 at 11:46pm
Hello all, I have this code I've been working on today and I'm stuck. I'm asking for help with, how to write this code to a file while using the 6 inputs I have for cost and payment(think its called reading to a file) and removing the zeros on the rows where there isn't any in the column. IE: 1.00 = 4 quarters 0 dimes 0 nickels 0 pennies. How do I include only the ones with values?

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
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    ofstream out;
    out.open("MakeChange.out");

    float cost, payment, change;
    int total_pennies;
    const int TWENTIES = 2000, TENS = 1000, FIVES = 500, ONES = 100,
              QUARTER = 25, DIME = 10, NICKEL = 5,PENNY = 1;

    cout << "You've entered the Make Change Program\n";

    cout << "Enter the COST: ";
     cin >> cost;
    cout << "Enter the PAYMENT: ";
     cin >> payment;

     change = payment - cost;

    if(payment < cost)
    {
       cout << "You need more money to purchase this item.";
    }
        if(cost == payment)
       {
          cout << "No change.";
       }
     else
     {
        cout << "\nYou have $ " << change << " in change.\n";

         total_pennies = change * 100;
        cout << "TOTAL PENNIES: " << total_pennies;

        cout << "\nTWENTIES: " << total_pennies / TWENTIES;
         total_pennies %= TWENTIES;

        cout << "\nTENS: " << total_pennies / TENS;
         total_pennies %= TENS;

        cout << "\nFIVES: " << total_pennies / FIVES;
         total_pennies %= FIVES;

        cout << "\nONES: " << total_pennies / ONES;
         total_pennies %= ONES;

        cout << "\nQUARTERS: " << total_pennies / QUARTER;
         total_pennies %= QUARTER;

        cout << "\nDIMES: " << total_pennies / DIME;
         total_pennies %= DIME;

        cout << "\nNICKELS: " << total_pennies / NICKEL;
         total_pennies %= NICKEL;

        cout << "\nPENNIES: " << total_pennies / PENNY;
         total_pennies %= PENNY;
     }
    out.close();
    return 0;
}


Inputs are
COST PAYMENT
.81 1.00
65.00 100.00
16.76 50.00
22.00 20.00
.25 100.00
42.05 42.05

OUTPUT
purchase .81 payment 1.00 change: 1 dime, 1 nickel, 4 pennies

Last edited on Sep 12, 2019 at 1:19am
Sep 12, 2019 at 2:07am
Calculate the number of each denomination and if it is not zero print it out (I commented out a few things that aren't used in this example):

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
#include <iostream>
// #include <fstream>
// #include <cmath>
// #include <iomanip>

int main()
{
   // ofstream out;
   // out.open("MakeChange.out");

   float /*cost, payment,*/ change;
   int pennies;
   const int TWENTIES = 2000, TENS = 1000, FIVES = 500, ONES = 100,
             HALF_DOLLAR = 50, QUARTER = 25, DIME = 10, NICKEL = 5;

   std::cout << "You've entered the Make Change Program\n";

   /*std::cout << "Enter the COST: ";
   std::cin >> cost;
   std::cout << "Enter the PAYMENT: ";
   std::cin >> payment;

   change = payment - cost;*/

   std::cout << "What's the change? ";
   std::cin >> change;
   std::cout << '\n';

   std::cout << "You have $ " << change << " change.\n";

   pennies = static_cast<int> (change * 100);
   std::cout << "(TOTAL PENNIES: " << pennies << ")\n\n";

   std::cout << "Your change is:\n";

   int num_twenties { pennies / TWENTIES };
   pennies %= TWENTIES;
   if (num_twenties) { std::cout << "TWENTIES: " << num_twenties << '\n'; }

   int num_tens { pennies / TENS };
   pennies %= TENS;
   if (num_tens) { std::cout << "TENS: " << num_tens << '\n'; }

   int num_fives { pennies / FIVES };
   pennies %= FIVES;
   if (num_fives) { std::cout << "FIVES: " << num_fives << '\n'; }

   int num_ones { pennies / ONES };
   pennies %= ONES;
   if (num_ones) {std::cout << "ONES: " << num_ones << '\n';}

   int num_halfies { pennies / HALF_DOLLAR };
   pennies %= HALF_DOLLAR;
   if (num_halfies) {std::cout << "HALF DOLLARS: " << num_halfies << '\n'; }

   int num_quarters { pennies / QUARTER };
   pennies %= QUARTER;
   if (num_quarters) { std::cout << "QUARTERS: " << num_quarters << '\n'; }

   int num_dimes { pennies / DIME };
   pennies %= DIME;
   if (num_dimes) { std::cout << "DIMES: " << num_dimes << '\n'; }

   int num_nickels { pennies / NICKEL };
   pennies %= NICKEL;
   if (num_nickels) { std::cout << "NICKELS: " << num_nickels << '\n'; }

   std::cout << "PENNIES: " << pennies << '\n';

   // out.close();
   // return 0;
}

You've entered the Make Change Program
What's the change? .81

You have $ 0.81 change.
(TOTAL PENNIES: 81)

Your change is:
HALF DOLLARS: 1
QUARTERS: 1
NICKELS: 1
PENNIES: 1

Why did I do a static_cast (line 31)?

To shut up a warning about a "conversion from 'float' to 'int', possible loss of data."
Sep 12, 2019 at 2:18am
Now, to finish answering your topic title question.....

If you can successfully get the correct output to the console, changing/adding output to a file is a trivial exercise.

You've already opened a file stream named "out" for writing to.

Change every use of std::cout that outputs the denominations to out and you are writing to your file.

OR copy those std::cout lines and change the copied line(s) to out.

That way you get output to the console window AND your file.

BTW, personally I would have created an enum instead of a bunch of const variables.
Sep 12, 2019 at 2:31am
Furry Guy,

This is a basic class, we aren't allowed to use while loop yet.
I guess I worded my question wrong. I need to understand how to take my inputs(from my paper) and have them within my code. I thought of making them all const but that would be a lot of code writing. There are 6 costs and 6 payments.

I need to read them into my code. or print them to file. I'm just either overthinking or just not grasping the concept yet.
Last edited on Sep 12, 2019 at 2:37am
Sep 12, 2019 at 2:37am
I said nothing about a while loop.

I showed how to correctly process ONE change making operation -- from console input, not your file -- the rest is up to you now.

If you are unable to use a while loop exactly what kind of loop are you allowed? Reading multiple file entries requires some form of a loop.

PLEASE don't say a GOTO loop.
Sep 12, 2019 at 2:40am
Apologies,

I wasn't meaning you said "while" I was just saying in this class we haven't covered that yet. So far we have " IF, IF/Else, ELSE IF, and SWITCH. I can't use anything else.

I appreciate your ONE change making operation. But I can't proceed any further until i can understand how to read or print to this output file my input data.
Sep 12, 2019 at 3:24am
Not sure what I am doing wrong. But all my outputs are wonky. Giving large values(8 places).




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
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    ofstream out;
    out.open("MakeChange.out");

    float cost, payment, change;
    int total_pennies;
    const int TWENTIES = 2000, TENS = 1000, FIVES = 500, ONES = 100,
              QUARTER = 25, DIME = 10, NICKEL = 5,PENNY = 1;

    cout << "You've entered the Make Change Program\n\n\n";

    cout << "Enter the COST: ";
     cin >> cost;
    cout << "Enter the PAYMENT: ";
     cin >> payment;

     change = payment - cost;

    if(payment < cost)
    {
       cout << "You need more money to purchase this item.";
    }
        if(cost == payment)
       {
          cout << "No change.";
       }
     else
     {
        cout << "\nYour change is: $ " << change;

         total_pennies = static_cast<int>(change * 100);


        int num_twenties;
        {
            total_pennies / TWENTIES;
        }
         total_pennies %= TWENTIES;
            if(num_twenties)
            {
                cout << "\nTWENTIES: " << num_twenties;
            }

        int num_tens;
        {
            total_pennies / TENS;
        }
         total_pennies %= TENS;
             if(num_tens)
             {
                 cout << "\nTENS: " << num_tens;
             }

      int num_fives;
      {
          total_pennies / FIVES;
      }
        total_pennies %= FIVES;
            if(num_fives)
            {
              cout << "\nFIVES: " << num_fives;
            }

      int num_ones;
      {
          total_pennies / ONES;
      }
        total_pennies %= ONES;
            if(num_ones)
            {
               cout << "\nONES: " << num_ones;
            }

     int num_quarters;
     {
         total_pennies / QUARTER;
     }
        total_pennies %= QUARTER;
        if(num_quarters)
        {
          cout << "\nQUARTERS: " << num_quarters;
        }


     int num_dimes;
     {
         total_pennies / DIME;
     }
        total_pennies %= DIME;
        if(num_dimes)
        {
           cout << "\nDIMES: " << num_dimes;
        }


     int num_nickels;
     {
         total_pennies / NICKEL;
     }
        total_pennies %= NICKEL;
        if(num_nickels)
        {
           cout << "\nNICKELS: " << num_nickels;
        }

      int num_pennies;
      {
          total_pennies / PENNY;
      }
        total_pennies %= PENNY;
        if(num_pennies)
        {
           cout << "\nPENNIES: " << num_pennies;
        }


     }
    out.close();
    return 0;
}


You've entered the Make Change Program


Enter the COST: 50
Enter the PAYMENT: 100

Your change is: $ 50
TWENTIES: 4199040
TENS: -1371538021
FIVES: 1975156762
ONES: 1975156771
QUARTERS: 8
DIMES: 1975219825
NICKELS: 7208668
PENNIES: 1975538752
Process returned 0 (0x0)   execution time : 7.457 s
Press any key to continue.
Last edited on Sep 12, 2019 at 3:26am
Sep 12, 2019 at 4:15am
1
2
3
4
int num_twenties; // <-- the semi-colon should NOT be here!
        {
            total_pennies / TWENTIES;
        }

is NOT the same as int num_twenties { pennies / TWENTIES };

How I create variables is called "uniform initialization." A feature introduced with C++11.

A different version of int num_twenties = pennies / TWENTIES ;

https://www.learncpp.com/cpp-tutorial/variable-assignment-and-initialization/

You are creating variables and NOT assigning any value to them. Your garbage output is because you are using variables full of garbage.
Topic archived. No new replies allowed.