numbers to long

my program works fine when the numbers stay below 10 digits in length, but once I hit an 10 or greater digit number the program bombs. I believe the problem is when I read the number from the file, like my variable can not hold all of the data but not sure how to fix it.

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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream infile;
ofstream outfile;
const int Max = 30;
int add(int[], int[], int[]);
int toArray(int, int[]);
int main()
{
    int num1[Max] = {};
    int num2[Max] = {};
    int sum[Max+1] = {};
    int N1, N2, i;
    int number1, number2;
    infile.open("C:\\Users\\Jacob\\Desktop\\BubbaAdd.txt");
    outfile.open("BAData.txt");
    i = 0;
    while(i < 7)
    {
        infile >> setprecision(Max) >> fixed >> number1 >> number2;
        N1 = toArray(number1, num1);
        N2 = toArray(number2, num2);
        add(num1, num2, sum);
        i++;
    }
    infile.close();
    outfile.close();
    return 0;
}
int toArray(int num, int ary[])
{
    int i;
    int x = Max - 1;
    while(num)
    {
        ary[x] = num % 10;
        num /= 10;
        x--;
    }
    for (i = 0; i < Max; i++)
    {
        if (ary[i] == 0)
        {
            if (ary[i-1] != 0 && ary[i+1] != 0)
            {
                cout << ary[i] << " ";
            }
            else
            {
                cout << "";
            }
        }
        else
        {
            cout << ary[i] << " ";
        }
    }
    cout << endl;
    return 0;
}
int add(int numb1[], int numb2[], int summ[])
{
    int i, j, k;
    int carry[Max] = {};
    int temp[Max] = {};
    for (k = Max; k > 0; k--)
    {
        if (numb1[k] + numb2[k] > 9)
        {
            carry[k-1] = carry[k-1] + 1;
        }
    }
    for (i = 0; i < Max; i++)
    {
        summ[i] = numb1[i] + numb2[i] + carry[i];
        if (summ[i] > 9)
        {
            summ[i] = summ[i] - 10;
        }
    }
    for (j = 0; j < Max; j++)
    {
        if (summ[j] == 0)
        {
            cout << " ";
        }
        else
        {
            cout << setprecision(Max+1) << fixed << summ[j] << " ";
        }
    }
    cout << endl;
}
Last edited on
Numeric limits: http://www.cplusplus.com/reference/limits/numeric_limits/

The int is not likely to be able to hold even all 10 digit numbers.
Topic archived. No new replies allowed.