Problems writing to file

Okay, so I have written an exercise using a number of functions to learn array manipulation. I've got the exact output I need and want using cout statements but now I want to dump everything to a "results.dat" file, which I have already created and opened. When running the program, however, when I change a cout << into an outFile <<, I get 'outFile' was not declared in this scope. You can see what I'm shooting for in the OutputTen function where I have tried to dump my array to my file instead of the console. Any ideas? Here's my code:

#include <iostream>
#include <fstream>

using namespace std;

const int size = 100; // Size of array

// Prototypes
void OpenFiles(ifstream&, ofstream&);
void OutputTen(int[]);
double Average(int[]);
void LargerThanAverage(int[], double);
void OutputEvenPositions(int[]);
void EvenValues(int[]);
void InvertValues(int[]);
void ArraySwap(int[]);
void NumOfMultiples(int[]);
void LargestValue(int[]);
void SmallestValue(int[]);
void ZeroArray(int[]);


// Functions
void OpenFiles(ifstream& inFile, ofstream& outFile) // Opens array.dat and opens/creates results.dat
{
inFile.open("array.dat");
outFile.open("results.dat");
}

void OutputTen(int array[])
{
for(int i = 0; i < 10; i++)
{
for(int j = (i*10); j < (i*10)+10; j++)
{
if(array[j] != 0)
outFile << array[j] << " ";
}
cout << endl;
}
cout << endl << "-----------------------------------------------------" << endl << endl;
}

double Average(int array[])
{
double sum = 0;
double average = 0;
for (int i = 0; i < size; i++)
{
if (array[i] != 0)
sum = sum + array[i];
}
average = sum / (size - 1);
cout << "The average of the values is: " << average << endl;
cout << endl << "-----------------------------------------------------" << endl << endl;

return average;
}

void LargerThanAverage(int array[], double average)
{
int lessThanCount = 0;
int greaterThanCount = 0;
for (int i = 0; i < size - 1; i++)
{
if (array[i] > average)
greaterThanCount++;
if (array[i] < average)
lessThanCount++;
}
cout << greaterThanCount << " values are greater than the average." << endl;
cout << lessThanCount << " values are less than the average." << endl;

cout << endl << "-----------------------------------------------------" << endl << endl;
}

void OutputEvenPositions(int array[])
{
for(int i = 0; i < 5; i++)
{
for(int j = i*20; j<(i*20)+20; j++)
{
if(array[j] != 0)
cout << array[j] << " ";
j++;
}
cout << endl;
}
cout << endl << "-----------------------------------------------------" << endl << endl;
}

void EvenValues(int array[])
{
for (int i = 0; i < size; i++)
{
if (array[i] % 2 == 0)
cout << array[i] << " ";
}
cout << endl << endl << "-----------------------------------------------------" << endl << endl;
}

void InvertValues(int array[])
{
for (int i = 0; i < size; i++)
{
array[i] = -array[i];
}
}

void ArraySwap(int array[])
{
int temp[1];
temp[0] = array[0];
array[0] = array[2];
array[2] = temp[0];

for (int i = 0; i < size; i ++)
cout << array[i] << " ";
cout << endl << endl << "-----------------------------------------------------" << endl << endl;
}

void NumOfMultiples(int array[])
{
int counter = 0;
for (int i = 0; i < size; i++)
{
if (array[i] % 4 == 0 && array[i] %7 == 0)
counter++;
}
cout << "There are " << counter << " values that are multiples of both 4 and 7." << endl;
cout << endl << "-----------------------------------------------------" << endl << endl;
}

void LargestValue(int array[])
{
int largest = array[0];
int location = 0;
for (int i = 0; i < size; i++)
{
if (array[i] > largest)
{
largest = array[i];
location = i;
}
}

cout << largest << " is the largest value and it is located at position " << location << " in the array."<< endl;
cout << endl << "-----------------------------------------------------" << endl << endl;
}

void SmallestValue(int array[])
{
int smallest = array[0];
int location = 0;
for (int i = 0; i < size; i++)
{
if (array[i] < smallest)
{
smallest = array[i];
location = i;
}
}

cout << smallest << " is the smallest value and it is located at position " << location << " in the array."<< endl;
cout << endl << "-----------------------------------------------------" << endl << endl;
}

void ZeroArray(int array[])
{
for (int i = 0; i < size; i++)
{
if (array[i] % 2 == 0)
array[i] = 0;
cout << array[i] << " ";
}
cout << endl << endl << "-----------------------------------------------------" << endl << endl;
}


int main()
{
int array[size];
ifstream inFile;
ofstream outFile;
double average;

inFile.open("array.dat");
outFile.open("results.dat");

//OpenFiles(inFile, outFile);
if(!inFile || !outFile)
{
cout << "Error opening files " << endl;
return 1;
}

for(int i = 0; i < size; i++)
{
inFile >> array[i];
}

cout << "Problem 1:" << endl << endl << "*** Read values into the array ***" << endl;
cout << endl << "-----------------------------------------------------" << endl << endl;
cout << "Problem 2:" << endl << endl;
OutputTen(array);
cout << "Problem 3:" << endl << endl;
average = Average(array);
cout << "Problem 4:" << endl << endl;
LargerThanAverage(array, average);
cout << "Problem 5:" << endl << endl;
OutputEvenPositions(array);
cout << "Problem 6:" << endl << endl;
EvenValues(array);
cout << "Problem 7:" << endl << endl << "*** Inverted the values in the array ***" << endl;
InvertValues(array);
cout << endl << "-----------------------------------------------------" << endl << endl;
cout << "Problem 8:" << endl << endl;
average = Average(array);
cout << "Problem 9:" << endl << endl;
ArraySwap(array);
cout << "Problem 10:" << endl << endl;
NumOfMultiples(array);
cout << "Problem 11:" << endl << endl;
LargestValue(array);
cout << "Problem 12:" << endl << endl;
SmallestValue(array);
cout << "Problem 13:" << endl << endl;
ZeroArray(array);
cout << "Problem 14:" << endl << endl;
average = Average(array);

inFile.close();
outFile.close();

return 0;
}
Last edited on
You have to pass the file pointer to every function you use it in.
Okay...I fixed it by moving my ifstream and ofstream declarations outside of my main method.
Topic archived. No new replies allowed.