Return to the BobbyGriggs.com C++ Nook


Chapter 7 - Even, Odd, or Negative?
© 1998 by Bobby Griggs.



// Program name:  Integers

// Purpose:  Output even, odd, or negative numbers out to different files

// for each category.



// Win32 console application



#pragma hdrstop



#include < iostream.h >

#include < stdio.h >

#include < fstream.h >

#include < stdlib.h >

#include < string.h >

#include < assert.h >



#define FILENAME "integers.dat"

#define OUTEVEN "even.dat"

#define OUTODD "odd.dat"

#define OUTNEGATIVE "negative.dat"



void Work();



void main()

{

	Work();

	return;

}



void Work()

{

	ifstream in_file;

	ofstream out1,out2,out3;

	char number[5];

	int num;

	

	in_file.open(FILENAME);

	assert(!in_file.fail());

	out1.open(OUTEVEN,ios::app);

	assert(!out1.fail());

	out2.open(OUTODD,ios::app);

	assert(!out2.fail());

	out3.open(OUTNEGATIVE,ios::app);

	assert(!out3.fail());

	while (!in_file.eof())

	{

		in_file.getline(number,sizeof(number));

		num = atoi(number);

		if (num % 2 == 0)

			out1 << number << endl;

		else

			out2 << number << endl;

		if (num < 0)

			out3 << number << endl;

	}

	in_file.close();

	out1.close();

	out2.close();

	out3.close();

	return;

}