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;
}

Does this help?

using namespace std;

const string FILENAME = "INTEGERS.TXT";
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&);
bool getNum(ifstream &inp, ofstream &otp);


int main()
{
	srand ((unsigned int)time(NULL));

	/* Populating INTEGERS.txt */
	ofstream outData(FILENAME.c_str());
	int base = -250;
	for (int i = 0; i < MAX_INT; i++)
		outData << base + rand()%500 << endl;
	outData.close();

	/* Processing INTEGERS.txt */
	ifstream inData;
	if (!openFileForInput(inData))
	{
		cout << INFILE_ERR_MSG;
		system("PAUSE");
		return EXIT_ERR_INFILE;
	}
	
	cout << endl << endl;
	system ("PAUSE");
	return 0;
}

// Open a file for input
// Check for existence of file in current directory
// If file exists, is there any data in the file
bool openFileForInput(ifstream& inp)
{
	inp.open(FILENAME.c_str()); // See if we can open
	inp.peek();  // Determine whether file is not empty
	return (inp && !inp.eof()); // checks if the file exists and not empty
}

bool getNum(ifstream &inp, ofstream &otp)
{
	//Let's start with 3 arrays.
	int neg[MAX_INT] = {0};
	int posEven[MAX_INT] = {0};
	int posOdd[MAX_INT] = {0};
	//Let's also use 3 size_t to store our
	//current index as we populate the arrays.
	size_t negativeSize, positiveEvenSize, positiveOddSize;
	negativeSize = positiveEvenSize = positiveOddSize = 0;

	int temp = 0;
	while(inp >> temp) //<-- this is a safe mechanism for reading the file.
	{
		//Using the index variables for the arrays while we read the file, we can 
		//populate the arrays.  Error checking is necessary though, or it will overflow your buffer!!
		
		if(temp == 0)
			continue;
		if(temp > 0)//Positive
		{
			if( temp % 2 )//Odd
			{
				posOdd[positiveOddSize] = temp;
				positiveOddSize++;
			}
			else//Even
			{
				posEven[positiveEvenSize] = temp;
				positiveEvenSize++;
			}
		}
		else//Negative
		{
			neg[negativeSize] = temp;
			negativeSize++;
		}
	}

	return false;
}

Oh and it is nice to see someone willing to do their own work rather than begging someone else to do it for them.

I think use the vector is better.

vector<int>  neg;
	vector<int> posEven;
	vecotor<int> posOdd;
if (input number<0) poseven.push_back(number);
else if( number>0 && is_Even(number) ) posEven.pub_back(number);

I think use the vector is better.

vector<int>  neg;
	vector<int> posEven;
	vecotor<int> posOdd;
if (input number<0) poseven.push_back(number);
else if( number>0 && is_Even(number) ) posEven.pub_back(number);

Yes I also agree that the vector is better, it's up to the poster to decide what he wants to use.

vector will not work because I haven't learned about them yet. So I have to do this using arrays.

vector will not work because I haven't learned about them yet. So I have to do this using arrays.

Did my post help you any?

commented: very helpful thank you +0

Did my post help you any?

Yes,thank you very much it was just the nudge I needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.