What am I missing in this second function?

I am pretty terrible with passing parameters, and I could use some help. My program reads in from a file and uses the numbers to manipulate an array. I have successfully input the values from the file and gotten them to print to an output file with a space in between. My problem now is figuring out how to simply print them now, 5 to a line, in the second function. How can I pass the array as a parameter and do this without having to read them all in again? Or can I? Also, can you help with syntax rules as far as passing parameters go? I never quite know which (the prototype, the function call, or the function definition gets which variables...) My program is below, but the second function, as written, does nothing. Why is that?
Any help is very appreciated.

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
//******************************************************************************************************
//This is a program that will manipulate an array.  The program should handle up to 100 integer numbers.
//The array will be passed as a parameter.
//******************************************************************************************************

#include <iostream>  //Header files
#include <cmath>
#include <fstream>

using namespace std;

//File descriptors
ifstream infile;
ofstream outfile;

//Prototypes
void readInValues();
int printOutValues();
int printOutTotal();
int printOutSum();
int printOutAvg();
int printOutMinMax();

 //Begin main function
 int main()
{
    infile.open("array.txt");//Open infile
    outfile.open("answers.out");//Open outfile
    if( !infile)//If input file did not open correctly
    {
        cout << "Error opening input file." << endl;//Display error message
        return 0;//Quit
    }

    if( ! outfile)//If outfile does not open
    {
        cout << "Error opening output file." << endl;//Display error message
        return 0;//Quit
    }
    readInValues();//Call function 1
    printOutValues();//Call function 2

    return 0;
}

void readInValues()//Function header 1
{
    int ary[100];//Declare array with up to 100 integer numbers
    int j = 0;//Declare variable to use as array position

    infile >> ary[j];//Read in data from infile
    while (infile)//While read is successful
    {
        outfile << ary[j] << " ";//Display j
        j = j + 1;//Increment j.
        infile >> ary[j];//Read in the new j, after increment
    }//Then fall out of loop, to begin again as long as read is successful

    cout << "All numbers from infile 'array.txt' have successfully" << endl
         << "been read in through Function 1." << endl;//Display end of function message

}//End of function 1

int printOutValues ()//Function header 2
{

    int j = 0;//Declare variable to use as array position
    int ary[100];//Declare array
    infile.open("array.txt");//Open infile to read from
    infile >> ary[j];//Read in data from infile
    while (infile)//While read is successful
    {
        for (j = 0; j < 0; j++) //J starts at 0, loop will end when j <0, at the end of each loop, j increments
            {
                cout << ary[j] << "  " ;  //Display value in array’s position j
                if((j + 1) % 5 == 0) //If (j+1)/5 = 0, (this should only happen every 5 values)
                {
                    cout << endl; //Start a new line
                    outfile << endl; //Echo
                }//Then fall out of loop, to begin again as long as read is successful
                cout << "All numbers from the array have printed out" << endl
                 << "successfully through Function 2." << endl;  //Display end of function message
            }
}

}
I put the comments in the code comments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Prototypes
int readInValues(int []);
int printOutValues(int [],int);
// etc..
    // In the main
    int array[100];
    int cnt(0);
    cnt = readInValues(array);//Call function 1
    printOutValues(array,cnt);//Call function 2
// etc...
int readInValues(int ary[])//Function header 1
{
// Remove the ary[100] in the function because the 
// one passed in is going to be used now

   // Return the number that you read so you know how many to print later
   return j;
}
//
int printOutValues (int ary[], int i)//Function header 2
{
// Remove the ary[100] in the function because the 
// one passed in is going to be used now
// Also remove the while loop and infile stuff 
I think the statement j<0 in your for loop is never true, since you set it = 0 and then increment it... you probably want it to be j<100? You could also use the same format as your first function and manually increment the j value each loop.
Topic archived. No new replies allowed.