big number in array

How can you store a 30 digit number (ex. 132546351365841354646135456789) from a file with one digit per place in an array?
You could read it into a string:

1
2
3
std::ifstream fin("file.txt");
std::string num;
fin >> num;
could you take the string and use a character array to take each digit one at a time?
This is what I have so far but I get an error on line 21. I'm not sure if I'm using the getline function correctly

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>
#include <string>
using namespace std;
ifstream infile;
ofstream outfile;
const int Max = 30;
int add(int[], int[], int[]);
int toArray(char, int[]);
int main()
{
    int num1[Max] = {};
    int num2[Max] = {};
    int sum[Max+1] = {};
    char number1, number2;
    infile.open("C:\\Users\\Jacob\\Desktop\\BubbaAdd.txt");
    outfile.open("BAData.txt");
    while(infile)
    {
        getline(number1);
        toArray(number1, num1);
        getline(number2);
        toArray(number2, num2);
        add(num1, num2, sum);
    }
    infile.close();
    outfile.close();
    return 0;
}
int toArray(char 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 << summ[j] << " ";
        }
    }
    cout << endl;
}
Getline streams the entire line for you so you must include the input stream.

so it should look like:

 
std::getline( infile , number1 );
Also you are trying to pass a string as a character when you call the toArray function. Another thing to mention is I think you are not understanding functions fully. There are more return types than just integers it is the value to be returned by the function. If there is no return it should be void. So your add function should be void but I also believe you want void for your toArray function because returning 0 doesn't really do much.

Topic archived. No new replies allowed.