I can't figure out what is wrong.

Some times it works, but bout every other time it crashes, sometimes before, sometimes after the cout output.

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

class myint{
    public: int * d; public: myint(){d = new int[1]; d[0] = 0; size = 1;} myint(const myint & a){d = new int[a.size]; size = a.size; for (int i = 0; i < size; ++i) d[i] = a.d[i];} ~myint(){delete [] d;  }
    int size;
    bool resize(int ns);
    bool operator ++()
        {
            int x = size - 1;
            for (; x>=0; x--)
                if (d[x] < 9)
                    {d[x]++; return true;}
                    //return d[x]++;
                else d[x] = 0;
            resize(size+1);
            d[0] = 1;
            for (int i = 1; i <= size; ++i)
                d[i] = 0;
            return true;
            return false;
        }
    bool cout()
        {
            for (int i = 0; i < size; ++i)
                std::cout << d[i];
        }
};

bool myint::resize(int ns)
{
    myint copy(*this);
    size = ns;
    delete [] d;
    d = new int [size];
    for (int i = 0; i < copy.size; ++i)
        d[i] = copy.d[i];
    return true;
}


using namespace std;

int main()
{
    myint x;
    //for (int i = 0; i < 23409; ++i)
    //for (int i = 0; i < 123; ++i)
    for (int i = 0; i < 10; ++i)
        {++x;}
    x.cout();
    cout << "\ncout.\n" << flush;
    return 0;
}


Thanks again appreciate any input.
line 17 should probably be < size (not <=)

Other than that, you have some logic errors. Assuming d[size-1] is the "ones" digit, then your resize() function will not work because it doesn't shift the digits over like it should. It also doesn't zero the highest digit like it should.

Other than that I only see style issues (for the love of god put some line breaks on line 4, and put braces around the loop on line 10)
dang I can't believe i missed that.
but I'm 0'ing the digits in the ++ function not in the resize function

thanks though
now it works..... but very very slowly :(
1
2
3
4
5
                for (; x>=0; x--)
                if (d[x] < 9)
                    {d[x]++; return true;}
                    //return d[x]++;
                else d[x] = 0;

you should use brace, like this:

1
2
3
4
5
6
                for (; x>=0; x--) {
                if (d[x] < 9)
                    {d[x]++; return true;}
                    //return d[x]++;
                else d[x] = 0;
                }


and what's this man?
1
2
3
4
            bool operator ++ ()
            //...
            return true;
            return false;
Last edited on
Also, when you have such problems, you should use the debugger (or valgrind). Those programs will tell you exactly how, where and why the program has crashed.

now it works..... but very very slowly :(

That's not surprising. resize(size+1); is a very, very, very (repeat as often as you like) bad idea.
I got most of the bugs worked out (I think), but its still only computing factorials up to 7! or 8!
Anyone know whats wrong?
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
#ifndef INTEGER_H
#define INTEGER_H

#include <string>

#define very long
#define big long
#define number int

very long get_int_size(very long n)
{//I think this is for positive ints only
    int tn = n;
    (n < 0) ? tn *=  -1 :1;
    very long counter = 0, divisor = 1;
    while( (++counter) && (divisor *= 10) )
        if ((tn/divisor) >= 1)
            continue;
        else break;
    return counter;
}

int nth_digit(int n, int d)
{
    int tn = n;
    if (tn<0)
        tn *= -1;  //do I need this?
    int s = get_int_size(tn);
    if (d > s || d < 1)
        return -1;
    int increment = 1,place_holder = 10, counter = 0;
    for (int i = 1; i < d; i++)
        {
            tn = tn - (increment * nth_digit(tn, i));
            place_holder *= 10;
            increment *=10;
        }
    do
        if (tn % place_holder == 0)
            return ((place_holder - (counter*increment)) %place_holder )/increment;
        while( ( (tn +=increment) && (++counter) ) && (tn <= (n + place_holder)) + 10 );
    return -2;
}


bool is_valid_string_for_integer(std::string s)
{
    if ( (s[0] != '-') && (!isdigit(s[0])))
        return false;
    int size = s.size(); int i = 1;
    while(size-1 > i++)
        if (!isdigit(s[i]))
            return false;
    return true;
}

class integer
{
    public:
    short * d;
    bool POS;
    public:
    integer()
    {d = new short[1]; d[0] = 0; size = 1; POS = true;}
    integer(const integer & a){POS = a.POS; d = new short[a.size]; size = a.size; for (int i = 0; i < size; ++i) d[i] = a.d[i];}
    integer(const very big &vln){POS = (vln >= 0) ? true:false; size = get_int_size(vln); d = new short[size]; for (very big number i = size-1; i >= 0; --i) d[i] = nth_digit(vln, size - i ); }
    ~integer(){delete [] d;  }
    int size;
    bool resize(very long ns);
    bool operator ++()
        {
            very big number x = size - 1;
            for (; x>=0; x--)
                if (d[x] < 9)
                    {d[x]++; return true;}
                else d[x] = 0;
            resize(size+1);
            d[0] = 1;
            for (very big number i(1); i < size; ++i)
                d[i] = 0;
            return true;
        }
    bool operator --()
        {
            if (POS)
                {
                    int i = size - 1;
                    for (i; i >= 0; --i)
                        {
                            if (d[i] >= 0)
                                {
                                    return --d[i];
                                }
                        }
                }

        }

    integer operator +(const integer & rhs)
        {
            integer copy(*this), r(rhs);
            for (integer i; i < rhs; ++i)
                ++copy;
            return copy;
        }
    integer operator - (const integer & rhs)
        {
            integer copy(*this);
            for (integer i; i < rhs; ++i)
                --copy;
            return copy;
        }
    bool operator <(const integer & rhs)
        {
            if (POS && !rhs.POS)
                return false;
            if (rhs.POS && !POS)
                return true;
            if (!POS && !rhs.POS)
                {
                    integer a(*this), b(rhs);
                    a.POS = true; b.POS = true;
                    return a > b;
                }
            if (size < rhs.size)
                return true;
            if (size > rhs.size)
                return false;
            if (*this == rhs)
                return false;
            for (int i = 0; i < size; ++i)
                {
                    if (d[i] > rhs.d[i])
                        return false;
                    if (rhs.d[i] > d[i])
                        return true;
                }
            return true;
        }

    bool operator >(const integer & rhs)
        {
            if (POS && rhs.POS)
                {
                    if (size > rhs.size)
                        return true;
                    if (size < rhs.size)
                        return false;
                    if (*this == rhs)
                        return false;
                    for (int i = 0; i < size; ++i)
                        {
                            if (rhs.d[i] > d[i])
                                return false;
                            if (d[i] > rhs.d[i]) return true;
                        }
                            //{std::cout << "i = " << i << ".\n"; std::cout << "d[i] = " << d[i] << ".\n"; std::cout << "rhs.d[i] = " << rhs.d[i] << ".\n"; return false;}

                    return true;
                }
            if (POS && !rhs.POS)
                return true;
            if (!POS && !rhs.POS)
                {
                    integer a(*this), b(rhs);
                    a.POS = true; b.POS = true;
                    return a < b;
                }
            if (!POS && rhs.POS)
                {
                    return false;
                }
        }
    integer operator *(const integer & rhs)
        {
            integer ov(*this), rv; integer r(rhs);
            rv.POS = true; ov.POS = true;
            for (integer i; i < rhs; ++i)
                rv = rv + ov;
            rv.POS = (rhs.POS == POS) ? true:false;
            return rv;
        }
    bool operator +=(const integer & rhs)
    {
        integer temp(*this);
        temp = *this + rhs;
        if (temp == *this + rhs)
            {
                *this = temp;
                return true;
            }
        else return false;
    }

    bool operator == (const integer & rhs)
    {
        if (POS != rhs.POS)
            return false;
        if (rhs.size != size)
            return false;
        for (int i = 0; i < size; ++i)
            if (d[i] != rhs.d[i])
                return false;
        return true;
    }

    bool operator = (const integer & rhs)
    {
        integer temp(rhs);
        if (temp==rhs)
            {
                POS = temp.POS;
                size = temp.size;
                for (int i = 0; i < size; ++i)
                    d[i] = temp.d[i];
                return true;
            }
        else return false;
    }

    bool cout()
        {
            if (!POS)
                printf("-");
            for (int i = 0; i < size; ++i)
                printf("%d", d[i]);
        }
};

bool integer::resize(very long ns)
{
    integer copy(*this);
    size = ns;
    delete [] d;
    d = new short [size];
    for (int i = 0; i < size; ++i)
        d[i] = copy.d[i];
    return true;
}
#endif
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
#include "integer.h"
#include <stdexcept>

using namespace std;

runtime_error Negative_Factorial("Negative factorial not supported");

bool clearbuf()
    {char ioch; do ;  while( (ioch = getchar() != '\n' ) ); return true;}

integer factorial(integer x);
//very big factorial(very big x);

int main()
{
    integer x, y, z;
    very big number xnum;
    bool b = true;

    while(b)
        {
            x = 0; x.POS = true;
            do{
                printf("Enter a number:\n>");
            }while(!scanf("%lld", &xnum) && clearbuf());
            try
                {
                    very big number ceiling = (xnum < 0) ? xnum * -1:xnum;
                    for (integer i; i < ((xnum < 0) ? -1 * xnum:xnum); ++i, ++x);
                    if (xnum < 0) x.POS = false;
                    z = factorial(x);
                    printf("You typed: "); x.cout(); printf("\n");
                    printf("%lld! = ", xnum); z.cout();
                    printf("\n\n");
                }
            catch (runtime_error & rt_e)
                {
                    printf("%s\n", rt_e.what());
                }

            do{
                printf("Continue? 1 for Yes 0 for No\n>");
            }while( (!scanf("%d", &b) && clearbuf()) || (b!=0 && b!=1));
        }
}

/*very big factorial(very big x)
{
    if (x < 0)
        throw Negative_Factorial;
    if (x == 0 || x == 1)
        return 1;
    return x*factorial(x-1);
}*/

integer factorial(integer x)
{
    if (x < 0)
        throw Negative_Factorial;
    if (x == (integer)0 || x == (integer)1)
        return 1;
    return x*factorial(x-1);
}
P.S. sometimes it outputs werid results like:
8! = 403225306

or

8! = 4032-14423


or sometimes it will crash on 8! but still others it gets it right as 8! = 40320.
Topic archived. No new replies allowed.