ARRAYS/FILES/FUNCTIONS: help please!

Hello!

So I have to write a program that
inputs numbers from a single line in a file into an array.
It then uses a function to check which numbers are even or odd, and errors out
negative numbers. The even or odd numbers are then put into an even or odd array.
Then, a second function, finds the highest, lowest, and average of the even and
odd arrays. A third function outputs the higher than average even and odd numbers.

my program so far:
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void evenorodd(ifstream& filenumbers, ofstream& outnumbers, ofstream& errorfile, const int inputnumbers[], const int arraymax, int evenarray[], int oddarray[], int& index); // a function to check for and store even and odd numbers into seperate arrays.

int main()
{
ifstream filenumbers;
ofstream outnumbers, errorfile;
const int arraymax = 10;
int index;
const int inputnumbers[arraymax] = {}; //an array to input numbers from a file
int evenarray[arraymax] = {};
int oddarray[arraymax] = {};


filenumbers.open("E://lab7number.txt");
outnumbers.open("outnumbers.txt");
errorfile.open("errorfile.txt");

if (!filenumbers) //checks if input file is accessible
{
cout << "The input file will not open. Please restart." << endl;
exit(1);
}

if (!outnumbers)
{
cout << "The output file was not created successfully.Please restart." << endl;
exit(1);

}

if (!errorfile)
{
cout << " The error file was not created successfully. Please restart." << endl;
}

while (filenumbers)
{
if (!outnumbers.eof())
{

evenorodd(filenumbers, outnumbers, errorfile, inputnumbers, arraymax, evenarray, oddarray, index);
}


}

filenumbers.close();
outnumbers.close();
errorfile.close();
system("pause");
return 0;
}

void evenorodd(ifstream& filenumbers, ofstream& outnumbers, ofstream& errorfile, const int inputnumbers[], const int arraymax, int evenarray[], int oddarray[], int& index)
{

{

for (int index = 0; index < arraymax; index++)

{
filenumbers >> inputnumbers[index];

if (inputnumbers[index] % 2 == 0)
{
evenarray[index] = inputnumbers[index];

}
else oddarray[index] = inputnumbers[index];
}

}
}




I don't know how to get the function to only read one line of the file (I tried a for loop, didn't work). also, I don't know what to initialize the even and odd arrays to, since I cant really know the exact numbers. im not allowed to use pointers or vectors...
its driving me nuts! ive been working on this for 5 hours!
Last edited on
Please use code tags
http://www.cplusplus.com/articles/z13hAqkS/

I don't know how to get the function to only read one line of the file


getline or istream.getline
http://www.cplusplus.com/reference/string/string/getline/?kw=getline
http://www.cplusplus.com/reference/istream/istream/getline/?kw=cin.getline

I don't know what to initialize the even and odd arrays to

i didn't look the code over too closely but probalby 0 or '\0'
Topic archived. No new replies allowed.