Big Numbers and Classes

Ok I am a bit new to c++, I started as a java and using classes is a bit diffrent, and some help would be great here is my code for all my files any help would be great.

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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/******************************************
 * Project:    BigNCalc
 * File:       BigN.h
 *****************************************/

#ifndef BIGN_H
#define	BIGN_H

#include <string>

using namespace std;

class BigN {
public:

    /**
     * Basic constructor.
     */
    BigN();

    /**
     * Removes the BigN from memory
     */
    ~BigN();

    /**
     * Creates a BigN with a left and right value
     */
    BigN(const string val);

    /**
     * Adds the current BigN to the inputed BigN
     */
    void BigNoppP(const BigN right);

    /**
     * Mltiplys the current BigN to the inputed BIgN
     */
    void BigNoppM(const BigN right);

    /**
     * Assigns a new value to the BigN
     */
    bool setBigN(const string val);

    /**
     * @return a string containing the values of BigN
     */
    string getBigN();

    /**
     * Prints out the BigN
     */
    void print(int width);

    string getAnswer();

private:

    const int Max = 10;
    bool overFlow = false;
    int digits[Max];
    int answer[Max];

    /**
     * Finds the first non-zero of the string of 
     */
    int FirstNZ(const BigN me);

    /**
     * Adds two BigN's
     */
    void add(BigN left, BigN right, BigN answer);

    /**
     * Multiplies two BigN's
     */
    void mult(BigN left, BigN right, BigN answer);

};

#endif	/* BIGN_H */

/********************************************
 * Project: BigNCalc
 * File:    BigN.cpp
 ********************************************/

#include "BigN.h"
#include <string>

using namespace std;

/**
 * Constructor to make a BigN full of 0's
 */
BigN::BigN() {
    for (int i = 0; i < Max; i++) {
        digits[i] = 0;
        answer[i] = 0;
    }
}

/**
 * Constructs a BigN with a value
 */
BigN::BigN(const string val) {
    int nZ = val.find_first_of("123456789");
    if (nZ != -1) {
        for (int c = val.length() - 1, j = 0; c >= nZ && !overFlow; c--, j++) {
            if (j == Max) {
                overFlow = true;
                return;
            } else {
                char l = val.at(c);
                digits[j] = l - '0';
            }
        }
    }
}

/**
 * Finds the first non-zero character of the BigN to trim leading 0's away
 */
int BigN::FirstNZ(const BigN me) {
    bool found = false;
    int rtv = 0;
    int i = Max - 1;
    while (!found) {
        if (me[i] != 0) {
            rtv = i;
            found = true;
        }
        --i;
        if (i == 0) {
            found = true;
        }
    }
    return rtv;
}

/**
 * Adds two BigN's together.
 */
void BigN::add(BigN left, BigN right, BigN answer) {
    int carry = 0;
    for (int i = 0; i < Max; i++) {
        int val = left[i] + right[i] + carry;
        int dig = val % 10;
        carry = val / 10;
        answer[i] = dig;
    }
    if (carry != 0) {
        overFlow = true;
    }
}

/**
 * Multiplies two BigN's together
 */
void BigN::mult(BigN left, BigN right, BigN answer) {
    int firstDigit = FirstNZ(left);
    int secondDigit = FirstNZ(right);
    if (firstDigit + secondDigit >= Max + 1) {
        overFlow = true;
        return;
    }
    int carry = 0;
    for (int d = 0; d <= firstDigit + 1; d++) {
        BigN temp;
        for (int i = 0; i < Max; i++) {
            temp[i] = 0;
        }
        for (int sD = 0; sD <= secondDigit; sD++) {
            int val = (left[d] * right[sD]) + carry;
            int rD = val % 10; // devise carry
            carry = val / 10;
            temp[sD + d] = rD;
        }
        add(answer, temp, answer);
        if (overFlow) {
            return;
        }
    }
}

/**
 * Detects the Multiply opperator calls the mult method.
 */
void BigN::BigNoppM(const BigN right) {
    BigN::mult(digits, right, answer);
}

/**
 * Detects the Add opperator calls the add method.
 */
void BigN::BigNoppP(const BigN right) {
    BigN::add(digits, right, answer);
}

/**
 * Sets the value of a BigN
 */
bool BigN::setBigN(const string val) {
    int nZ = val.find_first_of("123456789");
    if (nZ != -1) {
        for (int c = val.length() - 1, j = 0; c >= nZ && !overFlow; c--, j++) {
            if (j == Max) {
                overFlow = true;
                return;
            } else {
                char l = val.at(c);
                digits[j] = l - '0';
            }
        }
    }
}

/**
 * Returns the value of a BigN as a string
 */
string BigN::getBigN() {
    int first = FirstNZ(digits);
    string out;
    for (int i = first; i <= Max; i++) {
        out[i] = digits[i];
    }
    return out;
}

string BigN::getAnswer() {
    int first = FirstNZ(answer);
    string out;
    for (int i = first; i <= Max; i++) {
        out[i] = answer[i];
    }
    return out;
}

/**
 * Deleates a BigN from memory
 */
BigN::~BigN() {
    for (int i = 0; i < Max; i++) {
        digits[i] = 0;
        answer[i] = 0;
    }
}

/**
 * Prints out a BigN right justified
 */
void BigN::print(int width) {
    int first = FirstNZ(digits);
    for (int i = width; i >= 0; i--) {
        if (i > first) {
            cout << " ";
        } else {
            cout << digits[i];
        }
    }
}

/********************************************
 * Project:    BigNCalc
 * File:       main.cpp
 ********************************************/

#include <cstdlib>
#include "BigN.h"
#include <iostream>
#include <string>

using namespace std;

/**
 * Finds the first non-zero digit in the string.
 */
int findFirstDigit(string entry) {
    bool found = false;
    int rtv = 0;
    int i = entry.length() - 1;
    while (!found) {
        if (entry[i] != 0) {
            rtv = i;
            found = true;
        }
        --i;
        if (i == 0) {
            found = true;
        }
    }
    return rtv;
}


int main() {
    
    string left;
    string right;
    char opp;
    char again = 'Y';
    
    cout << endl;
    cout << "Hello, I am a calculator for large numbers, up to 500 digits, but allas though I can "
            << "only add and multiply.  I take input in this fashion ####### + or * ##########."
            << endl;
    do {
        BigN first;
        BigN second;
        BigN answer;
        cin >> left >> opp >> right;
        cout << endl << endl;
        
        first.setBigN(left);
        second.setBigN(right);

        switch (opp) {
            case '+':
                answer.setBigN(first.BigNoppP(right));
                break;

            case '*':
                answer.setBigN(first.BigNoppM(right));
                break;

            default:
                cout << "!!ERROR!! Sorry, I can only add and multiply." << endl;
        }
        // Output //
        int size = findFirstDigit(left);
        if (findFirstDigit(right) > size) {
            size = findFirstDigit(right);
        }

        cout << "  ";
        first.print(size);
        cout << endl;
        cout << opp << " ";
        second.print(size);
        cout << endl;

        for (int i = 0; i <= size; i++) {
            cout << "=";
        }
        cout << endl;
        answer.print(size);
        cout << endl << endl;
        // Reset //
        first.~BigN();
        second.~BigN();
        answer.~BigN();

        cout << "Wanna do another one, Y or N?? ";
        cin >> again;
        cout << endl;

    } while (again == 'y' || again == 'Y');

    return 0;
}


Netbeans gives these errors

BigN.h:75: error: ISO C++ forbids initialization of member ‘Max’
BigN.h:75: error: making ‘Max’ static
BigN.h:76: error: ISO C++ forbids initialization of member ‘overFlow’
BigN.h:76: error: making ‘overFlow’ static
BigN.h:76: error: ISO C++ forbids in-class initialization of non-const static member ‘overFlow’
main.cpp: In function ‘int main()’:
main.cpp:70: error: invalid use of void expression
main.cpp:74: error: invalid use of void expression



closed account (zb0S216C)
BigN::Max must be static and must have an associated const qualifier in-order for it to be initialized in that way. For data member initialization, you must use the constructor initialization list. This can be achieved like this:

1
2
3
4
5
6
7
8
9
class BigN
{
    public:
        BigN( );
};

BigN::BigN( ) : Max( 10 ), overflow( false ) // List begins after the colon.
{
}

The initialization list initializers must appear in the same order in which they are declared. The list invokes the constructor of each data member. Note that initialization is different from assignment.

In general, a data member can only be initialized on the same line as the declaration only if it has an associated static storage-specifier and an associated const qualifier.

Wazzak
Last edited on
ok Thanks for the tip i will make it constant that is what it should be any way.
Ok my code has changed alot still having issues tough here it is.

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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/******************************************
 * Project:    BigNCalc
 * File:       BigN.h
 ******************************************/

#ifndef BIGN_H
#define	BIGN_H

#include <string>
#include <iostream>
#include <istream>
#include <ostream>


using namespace std;

class BigN {
public:

    /**
     * Basic constructor.
     */
    BigN();

    /**
     * Adds the current BigN to the inputed BigN
     */
    bool operator+(const BigN right);

    /**
     * Mltiplys the current BigN to the inputed BIgN
     */
    bool operator*(const BigN right);

    /**
     * Assignes the value of a BigN
     */
   // bool operator=(string val);

    // define in and output //
    friend ostream & operator<< (ostream & out, BigN right);

    friend istream & operator>> (istream & in, BigN right);

private:
    // Variables //
    static const int MAX = 10;
    bool overFlow;
    int digits[MAX];
    // Methods //
    /**
     * Finds the first non-zero of the string of
     */
    int FirstNZ();

    /**
     * Assigns a new value to the BigN
     * @param val,
     */
    bool setBigN(const string val);

    /**
     * Adds two BigN's
     */
    void add(BigN left, BigN right, BigN &answer);

    /**
     * Multiplies two BigN's
     */
    void mult(BigN left, BigN right, BigN &answer);

    /**
     * Prints out the BigN
     * @param width, the number of characters to print
     */
    void print(int width);

};

#endif	/* BIGN_H */


/********************************************
 * Project: BigNCalc
 * File:    BigN.cpp
 ********************************************/
/**
 * Class File for the BigN Class
 */

#include "BigN.h"
#include <string>
#include <iostream>
#include <istream>
#include <ostream>

using namespace std;

/**
 * Constructor to make a BigN full of 0's
 */
BigN::BigN() {
    for (int i = 0; i < MAX; i++) {
        digits[i] = 0; //sets the arrays to 0
    }
}

/**
 * Constructs a BigN with a value
 */
BigN::BigN(const string val) {
    int nZ = val.find_first_of("123456789");
    if (nZ != -1) {
        for (int c = val.length() - 1, j = 0; c >= nZ && !overFlow; c--, j++) {
            if (j == MAX) { //numbers are too big
                overFlow = true;
                return;
            } else { //else within size limits
                char l = val.at(c);
                digits[j] = l - '0'; //assgin given value
            }
        }
    }
}

/**
 * Finds the first non-zero character of the BigN to trim leading 0's away
 */
int BigN::FirstNZ(const BigN me) {
    bool found = false;
    int rtv = 0;
    int i = MAX - 1;
    while (!found) {
        if (me.digits[i] != 0) {
            rtv = i; //cycle tell find value greater than 0
            found = true;
        }
        --i;
        if (i == 0) {
            found = true; //sets array as 0
        }
    }
    return rtv;
}

/**
 * Adds two BigN's together.
 */
void BigN::add(BigN left, BigN right) {
    BigN temp;
    int carry = 0;
    for (int i = 0; i <= MAX; i++) {
        int val = left.digits[i] + right.digits[i] + carry;
        int dig = val % 10; //determines value
        carry = val / 10; //determines carry
        temp.digits[i] = dig;
    }
    for (int j = 0; j <= MAX; j++) { //reassign values
        digits[j] = temp.digits[j];
    }
    if (carry != 0) { //if number is too big
        overFlow = true;
    }
}

/**
 * Multiplies two BigN's together
 * @pre values are >= 0
 * @param left, value on the left side of the opperator
 * @param right, value on the right side of the opperator
 * @param answer, resault of multiplying the left and right values
 * @return the Answer in a BigN
 */
void BigN::mult(BigN left, BigN right) {
    int firstDigit = FirstNZ(left);
    int secondDigit = FirstNZ(right);
    if (firstDigit + secondDigit >= MAX + 1) {
        overFlow = true;
        return;
    }
    int carry = 0;
    for (int d = 0; d <= firstDigit + 1; d++) {
        BigN temp;
        for (int sD = 0; sD <= secondDigit; sD++) {
            int val = (left.digits[d] * right.digits[sD]) + carry;
            int rD = val % 10; //determines value
            carry = val / 10; //determines carry
            temp.digits[sD + d] = rD;
        }
        add(*this, temp);
        if (overFlow) { //if number is too big
            return;
        }
    }
}

/**
 * Detects the Multiply opperator calls the mult method.
 * @param right, the BigN to multiply this one by
 */
bool BigN::operator*(const BigN right) {
    mult(digits, right);
}

/**
 * Detects the Add opperator calls the add method.
 * @param right, the BigN to add to this one
 */
bool BigN::operator+(const BigN right) {
    add(digits, right);
}

/**
 *
 * @param val
 * @return
 */
//bool BigN::operator=(const string val){
    setBigN(val);
}

/**
 * Sets the value of a BigN
 * @param val, inputed value as a string
 */
bool BigN::setBigN(const string val) {
    bool set = false;
    int nZ = val.find_first_of("123456789");
    if (nZ != -1) {
        for (int c = val.length() - 1, j = 0; c >= nZ && !overFlow; c--, j++) {
            if (j == MAX) {
                overFlow = true; //if number is too big
                return;
            } else {
                char l = val.at(c);
                digits[j] = l - '0'; //sets value
            }
        }
    }
}

/**
 * Prints out the BigN
 * @param width, the number of characters to print
 */
void BigN::print(int width) {
    for (int i = width; i >= 0; i--) { //trims 0
        cout << digits[i]; //print numbers
    }
}
/********************************************
 * Project:    BigNCalc
 * File:       main.cpp
 ********************************************/

#include <cstdlib>
#include "BigN.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

/**
 * Finds the first non-zero digit in the string.
 * @param entry, given string.
 * @return int possation of the first digit > 0.
 */
int findFirstDigit(string entry) {
    bool found = false;
    int rtv = 0;
    int i = entry.length() - 1;
    while (!found) {
        if (entry[i] != 0) { //trims leading 0's
            rtv = i;
            found = true;
        }
        --i;
        if (i == 0) {
            found = true;
        }
    }
    return rtv;
}

int main() {
    // Variables //
    char opp;
    char again = 'Y';
    // Interface //
    cout << endl;
    cout << "Hello, I am a calculator for large numbers, up to 500 digits, but allas though I can "
            << "only add and multiply.  I take input in this fashion ####### + or * ##########."
            << endl;
    do {
        BigN first;
        BigN second;
        BigN answer;
        cin >> left >> opp >> right;
        cout << endl << endl;
        // Process input //

        switch (opp) { //devise operator
            case '+':
                answer = first + second;
                break;

            case '*':
                answer = first * second;
                break;

            default:
                cout << "!!ERROR!! Sorry, I can only add and multiply." << endl;
        }
        // Output //
        cout << "  ";
        cout << first;
        cout << endl; //format output
        cout << opp << " ";
        cout << second;
        cout << endl;
        cout << "===================";
        cout << endl;
        cout << answer;
        cout << endl << endl;


        cout << "Wanna do another one, Y or N?? "; //reset program to do another
        cin >> again;
        cout << endl;

    } while (again == 'y' || again == 'Y');

    return 0;
}



main.cpp: In function ‘int main()’:
main.cpp:67: error: no match for ‘operator=’ in ‘answer = first.BigN::operator+(second)’
BigN.h:24: note: candidates are: BigN& BigN::operator=(const BigN&)
main.cpp:71: error: no match for ‘operator=’ in ‘answer = first.BigN::operator*(second)’
BigN.h:24: note: candidates are: BigN& BigN::operator=(const BigN&)
Thanks for the help it works now but the source code is too big so if you want it let me know.
Topic archived. No new replies allowed.