Inheritance Problem

Hi guys,

I'm having a problem which involves inheritance. I am trying to prompt the user to input dollars and then cents. Once they input the information I prompt the user to input when they would like to add a percentage or subtract a percent (interest) to the amount they initially inputted. So for example, if you input 2 dollars and 50 cents, and then you select to subtract an interest of 50% then the output should be 1 dollar and 25 cents. However, it continues to display the same balance as the amount that was initially inputted. Could someone please help me ? Thank you

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

class Account
{
private:
    int Dollars;
    int Cents;
public:
    // Constructor
    Account();
    Account(int, int);
    
    // Deposit -> balance(Mutator)
    void deposit(int, int);
    void SubtractDeposit(int,int);
    
    // Accessor
    const int getDollars() const;
    const int getCents() const;
};

Account::Account()
{
    Dollars = 0;
    Cents = 0.0;
    
}
Account::Account(int d, int c)
{
    Dollars = d;
    Cents = c;
    
}
void Account::deposit(int d, int c)
{
    Dollars += (c/100) + d;
    Cents = (c%100) + Cents;
    
}

void Account::SubtractDeposit(int d, int c)
{
    Dollars -= d;
    Cents -= c;
    
}

const int Account::getDollars() const
{
    return Dollars;
}

const int Account::getCents() const
{
    return Cents;
}

class SavingsAccount: public Account
{
private:
    int DollarInterest;
    int CentInterest;
public:
    // Constructor
    SavingsAccount();
    SavingsAccount(int,int);
    // Mutator
    void addInterest(int,int);
    void SubtractInterest(int,int);
    const int GetDollarInterest() const;
    const int GetCentInterest() const;
};

SavingsAccount::SavingsAccount()
{
    DollarInterest = 0;
    CentInterest = 0;
    
    
}

SavingsAccount::SavingsAccount(int DI, int CI)
{
    DollarInterest = DI;
    CentInterest = CI;
    
}

void SavingsAccount::addInterest(int d, int c)
{
   
    DollarInterest = getDollars() * (d/100);
    CentInterest = getCents() * (c/100);
    deposit(DollarInterest, CentInterest);
    
}

void SavingsAccount::SubtractInterest(int d, int c)
{
    
    DollarInterest = getDollars() * (d/100);
    CentInterest = getCents() * (c/100);
    SubtractDeposit(DollarInterest, CentInterest);
    
}


int main()
{
    int Dollarz;
    int Cents;
    int DollarInterest;
    int CentInterest;
    int Select;
    
    
    
    cout << "Input Dollars: $";
    cin >> Dollarz;
    cout << endl;
    
    
    cout << "Input Cents: ¢";
    cin >> Cents;
    cout << endl;
    
    SavingsAccount myAccount;
    
    
    myAccount.deposit (Dollarz,Cents);
    
    
    cout << "Current Amount of Dollars: $" << myAccount.getDollars() << endl;
    cout << "Current Amount of Cents: ¢" << myAccount.getCents() << endl;
    
    cout << endl << endl;
    
    cout << "Menu: \n";
    cout << "==========;\n\n";
    cout << "1) Add Interest: \n";
    cout << "2) Subtract Interest: \n";
    cin >> Select;
    
    cout << endl << endl;
    
    while (Select <= 0 || Select > 2)
    {
        cout <<"Invalid entry, please re-enter: ";
        cin >> Select;
    }
    
    cout << endl;
    
    if (Select == 1)
    {
        cout << "Input how much interest you would like to add: %";
        cin >> DollarInterest;
        
        CentInterest=DollarInterest;
        
        cout << endl;
        
        myAccount.addInterest(DollarInterest,CentInterest);
        
        cout << "You added %" << DollarInterest << " of interest. ";
        
        cout << endl << endl;
        
        cout << "Current Amount of dollars with interest: $" << myAccount.getDollars() << endl;
        cout << "Current Amount of cents with interest: ¢" << myAccount.getCents() << endl;
    }
    
    cout << endl;
    
    if (Select == 2)
    {
        cout << "Input how much interest you would like to subtract: %";
        cin >> DollarInterest;
        
        CentInterest=DollarInterest;
        
        cout << endl;
        
        myAccount.SubtractInterest(DollarInterest,CentInterest);
        
        cout << "You added %" << DollarInterest << " of interest. ";
        
        cout << endl << endl;
        
        cout << "Current Amount of dollars subtracted after interest: $" << myAccount.getDollars() << endl;
        cout << "Current Amount of cents subtracted after interest: $" << myAccount.getCents() << endl;
        
        
    }
    
    
    
    return 0;
}
Hi,

You have integer division: 50/100 = 0


If you use a debugger you can deduce how this problem happens. Hopefully there is a GUI debugger in your IDE. Set up a watch list of variables, walk the code 1 line at a time, see how the values change.

Good Luck !!
Last edited on
Hi IdeasMan,

Sorry for the late reply. I will definitely give that try and see how it works! I have xcode which probably isn't the best IDE to have but if necessary i will download a more efficient one. Thank you so much for your help!
Topic archived. No new replies allowed.