infile to array

I need to read from a file with multiple number sets. The number sets are not necessarily as long as the array, so I would need to have the start of the array empty and fill until the end of the array.

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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream infile;
ofstream outfile;
int main()
{
    const int Max = 25;
    int num1[Max], num2[Max], sum[Max+1];
    int i, j, k;
    infile.open("BubbaAdd.txt");
    outfile.open("BAData.txt");
    for (j = 0; j < Max; j++)
    {
        num1[j] = 0;
    }
    for (i = Max; i > 0; i--)
    {
        infile >> num1[i];
        cout << num1[i];
    }
    infile.close();
    outfile.close();
    return 0;
}
I hope you realise you have not actually stated what the problem is. You have just said what the program does (cool story btw).

You can eliminate the need to manually initialise your array by using aggregate initialization:

int num1[Max] = {}; // will initalise the array to zeros
Topic archived. No new replies allowed.