Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to get this program to open my input file and open my output file an

ID: 3627053 • Letter: I

Question

I am trying to get this program to open my input file and open my output file and it is not doing it. What am I doing wrong?
My input txt file "Ch7_Ex5Data.txt" :
43 67 82 0 35 28 -64 7 -87 0 0 0 0 12 23 45 7 -2 -8 -3 -9 4 0
1 0 -7 23 -24 0 0 12 62 100 101 -203 -340 500 0 23 0 54 0 76

Here is my program:

#include <iostream>
#include <fstream>
#include <iomanip>
#include<cstdlib>
using namespace std;


//Function prototypes
void initialize(int& zeroCount, int& oddCount, int& evenCount);
void getNumber( ifstream& in, ofstream& out, int& num);
void classifyNumber(int num, int& zeroCount, int& oddCount,int& evenCount);
void printResults( ofstream& out, int zeroCount, int oddCount, int evenCount, int sum );



// initializes the variables
void initialize(int& zeroCount, int& oddCount, int& evenCount)
{
zeroCount = 0;
oddCount = 0;
evenCount = 0;
}
// reads and then writes a numer from/to the file
void getNumber( ifstream& in, ofstream& out, int& num)
{
// read a number from input file
if (( in >> num ) != NULL )
out << num << " "; // write to out put file
} // end funtion getNumber


// classifies a given number
void classifyNumber ( int num, int& zeroCount, int& oddCount, int& evenCount )
{
switch( num% 2)
{
case 0 :
evenCount++;
if (num == 0)
zeroCount++;
break;

case 1:
case -1:
oddCount++;
} // end case
} // end funtion classifyCount


// output the results to output-file
void printResults ( ofstream& out, int zeroCount, int oddCount, int evenCount, int sum)
{
// compute the average
double avg = (sum * 1.0) / ( oddCount + evenCount );

// print the results to the output file

out << " There are " << evenCount << " evens" << " which includes"<< zeroCount << " zeros.";
out << " The number of odd numbers is: " << oddCount;
out << " The sum of the above numbers is " << sum;
out << " The average3 of the above numbers is " << avg;
system("pause");

} // end funtion printResults




int main ()
{


//Variable declaration
int counter;
int number;
int zeros;
int odds;
int evens;
int sum = 0;
ifstream infile;
ofstream outfile;






// let user know about program
cout <<" Program that classifies numbers.";

// open the input file
infile.open("Ch7_Ex5Data.txt");

//Display error when the file was not opened
if (!infile.eof())
{
cout << " Error in opening input file";
return 1;
} // end if


// open output file
outfile.open("Ch7_Ex5Out.txt");

if (!outfile.is_open())

// Display error when the file was not opened
{
cout << " Error in opening ouput file.";
}
// initialize the variables
initialize(zeros, odds, evens); //Step 1

// read numbers from file and classify
counter = 0;
while(true)
{
// read a number from files and write to outfile
getNumber( infile, outfile, number );

// break the loop when all data was read
if (!infile )
break;

// keep track of sum and count of the numbers
sum += number;
counter++;

// insert a new line for every 10 numbers
if (( counter % 10 ) == 0 )
outfile << endl;

// classify the number
classifyNumber (number, zeros, odds, evens );

} // end while


//print the final results in output files
printResults (outfile, zeros, odds, evens, sum);

infile.close();
outfile.close();

return 0; // indicate program executed successfully
} // end of funtion, main

Explanation / Answer

#include #include #include #include using namespace std; //Function prototypes void initialize(int& zeroCount, int& oddCount, int& evenCount); void getNumber( ifstream& in, ofstream& out, int& num); void classifyNumber(int num, int& zeroCount, int& oddCount,int& evenCount); void printResults( ofstream& out, int zeroCount, int oddCount, int evenCount, int sum ); // initializes the variables void initialize(int& zeroCount, int& oddCount, int& evenCount) { zeroCount = 0; oddCount = 0; evenCount = 0; } // reads and then writes a numer from/to the file void getNumber( ifstream& in, ofstream& out, int& num) { // read a number from input file if (( in >> num ) != NULL ) out
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote