Hi,
I'm stuck... I'm writing a program that creates a data file then populates the file with 500 random integers both positive and negative. The problem I'm having is reading the integers from the file and storing them into three arrays for processing neg[],posEven[],and posOdd[]. I really just need a nudge in the right direction for creating this function as I've hit a wall and don't know where to start. I thought about using a nested if/else for sorting but don't know if a for loop is better.
here's what I have so far thanks in advance for any help I do appreciate it.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
#include <ctime>
using namespace std;
const string AUTHOR = "Rf";
const string ROLE = ", (s)\n";
const string SOLTXT = "\nSolution by ";
const string INFILE_ERR_MSG = "File does not exist or it is empty!\n";
const int EXIT_ERR_INFILE = -1; // Error with input file
const int MAX_INT = 500;
bool openFileForInput(ifstream&);
int main()
{
ofstream outData("INTEGERS.TXT");
time_t t;
time(&t);
srand (t);
int base = -250;
int num;
for (int i = 0; i < MAX_INT; i++)
{
outData << (num = base + rand()%500) << endl;
}
outData.close();
ifstream inData;
// Open a file for input
// Check for existence of file in current directory
// If file exists, is there any data in the file
if (!openFileForInput(inData))
{
cout << INFILE_ERR_MSG;
system("PAUSE");
return EXIT_ERR_INFILE;
}
cout << endl << endl;
system ("PAUSE");
return 0;
}
bool openFileForInput(ifstream& inp)
{
char check;
inp.open("INTEGERS.TXT"); // See if we can open
check = inp.peek(); // Determine whether file is not empty
if (inp && !inp.eof() ) // checks if the file exists and not empty
return true;
return false;
} /* End of openFileForInput() */
bool getNum(ifstream& inp, ofstream& otp)// this is where I'm stuck
{
int num;
unsigned int neg[];
int posEven[];
int posOdd[]
inp >> num;
if (inp && !inp.eof())
{
cin >> inp >> int posEven[num]
return true;
}
return false;
}