Structs problem

I think i am getting confused with passing structs and functions all in the same

When I run through the program (it compiles), the functions that add coins do not add, but rather just replace an old value with a new one.

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
#include <cstdlib>
#include <iostream>

using namespace std;

struct coinbox
{
       int quarters;
       int dimes;
       int nickels;
       int pennies;
};

void initialize (coinbox& c);
void add_quarters (coinbox& c, int quarters);
void add_dimes (coinbox& c , int dimes);
void add_nickels (coinbox& c, int nickels);
void add_pennies (coinbox& c, int pennies);
void make_change (coinbox& c, int amount);
void show (coinbox c);



int main(int argc, char *argv[])
{
   
    
    char input;
    coinbox  c,q,d,n,p;
    
  
    initialize(c);
    
     bool coins = true;
     while (coins==true)
     {
    
    cout << "Enter s (show coinbox), q (add quarters), d (add dimes)\n n (add nickels), p (enter pennies), c (make change), e(end): ";
    cin >> input;
    
    
    
    if (input=='s')
        {
        show (c);
        }
        
    if (input=='q')
       {
       add_quarters (c, q.quarters);
       }
    
    if (input=='d')
       {
       add_dimes (c, d.dimes);
       }
       
    if (input=='n')
       {
       add_nickels (c, n.nickels);
       }
       
    if (input=='p')
       {
       add_pennies (c, p.pennies);
       }

    
    
       
    
    if(input=='e')
    {
     coins=false;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
   
}

}

void initialize (coinbox& c)
{
     c.dimes=0;
     c.quarters=0;
     c.nickels=0;
     c.pennies=0;
     
}

void add_quarters (coinbox& c, int quarters)
{
     
     cout << "Enter the number of quarters to add: ";
     cin >> c.quarters;
     cout << endl;
     
     }
     
     
void add_dimes (coinbox& c , int dimes)
{
     cout << "Enter the number of dimes to add: ";
     cin >> c.dimes;
     cout << endl;
     
     
     }
     
     
void add_nickels (coinbox& c, int nickels)
{
     cout << "Enter the number of nickels to add: ";
     cin >> c.nickels;
     cout << endl;
     
     
     }
     
     
void add_pennies (coinbox& c, int pennies)
{
     cout << "Enter the number of pennies to add: ";
     cin >> c.pennies;
     cout << endl;
     
     }
     
     
void make_change (coinbox& c, int amount)
{
     
     }


void show (coinbox c)
{
     cout << "\nQuarters: "<< c.quarters;
     cout << "\nDimes: "<< c.dimes;
     cout << "\nNickels: " << c.nickels;
     cout << "\nPennies: " << c.pennies;
     cout << endl;
     cout << endl;
     
     }
I'm a little confused regarding what these functions are supposed to do.

1
2
3
4
5
6
void add_pennies (coinbox& c, int pennies)
{
    cout << "Enter the number of pennies to add: ";
    cin >> c.pennies;
    cout << endl;     
}


Decide on only ONE thing for a function to do. If you want the user to input a value and add that value to a coinbox do this:
1
2
3
4
5
6
7
void add_pennies (coinbox& c)
{
    int newPennies;
    cout << "Enter the number of pennies to add: ";
    cin >> newPennies;
    c.pennies += newPennies;
}


if you want to add pennies from an argument to a coinbox, do this:
1
2
3
4
void add_pennies (coinbox& c, int pennies)
{
    c.pennies += pennies;
}


if you want to dump one coinbox into another, do this:
1
2
3
4
5
6
7
void dump_box(coinbox& dest, coinbox source)
{
    dest.quarters += source.quarters;
    dest.dimes    += source.dimes;
    dest.nickels  += source.nickels;
    dest.pennies  += source.pennies
}


On some compiler settings, an unreference parameter (like parameter 2 of add_pennies) will give a warning. That's because it's poor design. You don't want to force a user of a function to give data that isn't used/needed. They may need to go to some serious calculations to get you that data. In addition, it isn't clear what it is intended for anyways.
Last edited on
Topic archived. No new replies allowed.