Comparing Class Values

I need help comparing values of a class, but the bool methods isn't worker. I think my logic is correct the code is wrong.
here's the method
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool piggybank::isLessthan(piggybank p2)
{
    
    if(printPiggyBankValue() < p2.printPiggyBankValue())
        return true;
    else
        return false;
    
}

bool piggybank::isGreaterthan(piggybank p2)
{
    if(printPiggyBankValue() > p2.printPiggyBankValue())
        return true;
    else
        return false;
}

piggybank piggybank::sumPiggyBanks(piggybank p2)
{
    return PiggyBank(penny_num+p2.penny_num, nickel_num+p2.nickel_num, dime_num+p2.dime_num, quarter_num+p2.quarter_num);
}



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
/**********************************************************************
 CSCI 240         Program 10     Fall 2014
 
 Programmer: Benjamin Blecha
 
 Section:0001
 
 Date Due: December 5, 2014
 
 Purpose:  This displays Gelevators moving up and down floors.
 **********************************************************************/


//include library
#include <iostream>
#include <iomanip>
#include <string>

//using namespace std
using namespace std;

class piggybank
{
public:
    piggybank();
    piggybank( int, int, int, int);
    void printPiggyBank();
    void printPiggyBankValue();
    void emptythebank();
    void addcoins( int, int, int, int);
    void addpennies(int);
    void addnickels(int);
    void adddimes(int);
    void addquarters(int);
    bool isEqual(piggybank);
    bool isLessthan(piggybank);
    bool isGreaterthan(piggybank);
    piggybank sumPiggyBanks(piggybank);
    
    
private:
    int penny_num;
    int nickel_num;
    int dime_num;
    int quarter_num;
    double penny = 0.01;
    double nickel = 0.05;
    double dime = 0.10;
    double quarter = 0.25;
};

piggybank::piggybank()
{
    penny_num = 0;
    nickel_num = 0;
    dime_num = 0;
    quarter_num =0;
}

piggybank::piggybank( int penny_count, int nickels_count, int dime_count, int quarter_count)
{
 if (penny_count < 0)
     cout << "Constructor error: the number of pennies cannot be negative."<< endl;
    else
        penny_num = penny_count;
if ( nickels_count < 0)
    cout << "Constructor error: the number of nickels cannot be negative." << endl;
    else
        nickel_num = nickels_count;
if ( dime_count < 0)
        cout << "Constructor error: the number of dimes cannot be negative." << endl;
   else
    dime_num = dime_count;
if ( quarter_count < 0)
    cout << "Constructor error: the number of quarters cannot be negative." << endl;
    else
    quarter_num = quarter_count;
}

void piggybank::printPiggyBank()
{
    cout << "Pennies:   " << penny_num << "   Nickels:   " << nickel_num << "   Dimes:   " << dime_num << "   Quarters:   " << quarter_num << endl;
    
}

void piggybank::printPiggyBankValue()
{
    double sum = 0;
    sum = (penny_num * penny) + ( nickel_num * nickel) + (dime_num * dime) + (quarter_num * quarter);
    cout << "$" << sum;
}

void piggybank::emptythebank()
{
    penny_num = 0;
    nickel_num = 0;
    dime_num = 0;
    quarter_num =0;
}

void piggybank::addcoins( int penny_count, int nickels_count, int dime_count, int quarter_count)
{
    if (penny_count < 0)
        cout << "Error: the number of pennies cannot be negative."<< endl;
    else
        penny_num = penny_count+ penny_num;
    if ( nickels_count < 0)
        cout << "Error: the number of nickels cannot be negative." << endl;
    else
        nickel_num = nickels_count + nickel_num;
    if ( dime_count < 0)
        cout << "Error: the number of dimes cannot be negative." << endl;
    else
        dime_num = dime_count + dime_num;
    if ( quarter_count < 0)
        cout << "Error: the number of quarters cannot be negative." << endl;
    else
        quarter_num = quarter_count + quarter_num;
}

void piggybank::addpennies(int addpenny)
{
    if (addpenny < 0)
        cout << "Error: the number of pennies cannot be negative."<< endl;
    else
        penny_num = addpenny+ penny_num;
}

void piggybank::addnickels(int addnickel)
{
    if ( addnickel < 0)
        cout << "Error: the number of nickels cannot be negative." << endl;
    else
        nickel_num = addnickel+ nickel_num;
}

void piggybank::adddimes(int adddimes)
{
    if ( adddimes < 0)
        cout << "Error: the number of dimes cannot be negative." << endl;
    else
        dime_num = adddimes + dime_num;
}

void piggybank::addquarters(int addquarter)
{
    if ( addquarter < 0)
        cout << "Error: the number of quarters cannot be negative." << endl;
    else
        quarter_num = addquarter + quarter_num;
}

bool piggybank::isEqual(piggybank p2)
{
    if(penny_num == p2.penny_num
       && dime_num == p2.dime_num
       && nickel_num == p2.nickel_num
       && quarter_num == p2.quarter_num)
        return true;
    else
        return false;
}

bool piggybank::isLessthan(piggybank p2)
{
    
    if(printPiggyBankValue() < p2.printPiggyBankValue())
        return true;
    else
        return false;
    
}

bool piggybank::isGreaterthan(piggybank p2)
{
    if(printPiggyBankValue() > p2.printPiggyBankValue())
        return true;
    else
        return false;
}

piggybank piggybank::sumPiggyBanks(piggybank p2)
{
    return PiggyBank(penny_num+p2.penny_num, nickel_num+p2.nickel_num, dime_num+p2.dime_num, quarter_num+p2.quarter_num);
}
1
2
3
4
5
6
void printPiggyBankValue();
// and
if ( foo.printPiggyBankValue() < bar.printPiggyBankValue() )

// mean:
if ( void < void )

... and that is an error.

You do need numbers, but you don't want to print anything there.
Last edited on
Topic archived. No new replies allowed.