Hello,
I am new to programming. I am trying to write a code that uses a file to input an undetermined amount of integers and separates the evens, odds, and zeros and outputs into a file the count of zeros, evens, and odds as well as the sum and average of the entered numbers. I am extremely confused. I am attaching my code below... can anyone point me in the right direction? I have 11 errors, mostly undeclared identifiers????. I have posted those below the code. I'm not looking for the answer, but more of a clue of how to correct this so I can understand it. There are so many variables and parameters and arguments that my head is spinning! I am so confused. Any clarity would be appreciated.
Thank you.
CODE:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//Function Protoypes
void initialize(int& zeroCount, int& oddCount, int& evenCount);
int getNumber();
void classifyNumber(int num,int& zeroCount, int& oddCount, int& evenCount);
void average(int sumCount);
void average(int sumCount);
void printResults(int zeroCount, int oddCount, int evenCount, int sum, int avg);
//Main Function
int main()
{
//File stream variable declarations
ifstream inFile;
ofstream outFile;
//Variable Declarations
int number; //variable to stoer new number
int zeros; //variable to store the number of zeros
int odds; //variable to store the number of odd numbers
int evens; //variable to store the number of even numbers
//Open Files
inFile.open("C:\\Users\\Kristina\\Documents\\School\\CS217A\\inDataCH7.txt");
outFile.open("C:\\Users\\Kristina\\Documents\\School\\CS217A\\outDataCH7.txt");
initialize(zeros, odds, evens);
while (inFile)
{
number = getNumber();
}
classifyNumber(number, zeros, odds, evens);
printResults(zeros, odds, evens, sum, average);
//Close files
inFile.close();
outFile.close();
return 0;
}
void initialize(int& zeroCount, int& oddCount, int& evenCount)
{
zeroCount = 0;
oddCount = 0;
evenCount = 0;
}
int getNumber()
{
int num;
infile >> num;
outfile << setw(10) << num << " ";
return num;
}
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 switch
} //end classifyNumber
void sum(int zeros, int odds, int evens)
{
int z, o, e;
sum = z + o + e;
}
void average(int sumCount)
{
int average;
average = sum /(zeroCount + evenCount + oddCount);
}
void printResults(int zeroCount, int oddCount, int evenCount, int sum, int average)
{
outfile << "There are " << evenCount << " evens, "
<< "which includes " << zeroCount << " zeros"
<< endl;
outfile << "The number of odd numbers is: " << oddCount
<< endl;
outfile << "The sum of the numbers is: " << sum << endl;
outfile << "The average of the numbers is: " << average << endl;
} //end printResults
ERRORS:
1>c:\users\kristina\documents\visual studio 2008\projects\s4 ch7 assignment\s4 ch7 assignment\s4 ch7 assignment.cpp(54) : error C2065: 'sum' : undeclared identifier
1>c:\users\kristina\documents\visual studio 2008\projects\s4 ch7 assignment\s4 ch7 assignment\s4 ch7 assignment.cpp(74) : error C2065: 'infile' : undeclared identifier
1>c:\users\kristina\documents\visual studio 2008\projects\s4 ch7 assignment\s4 ch7 assignment\s4 ch7 assignment.cpp(75) : error C2065: 'outfile' : undeclared identifier
1>c:\users\kristina\documents\visual studio 2008\projects\s4 ch7 assignment\s4 ch7 assignment\s4 ch7 assignment.cpp(98) : error C2659: '=' : function as left operand
1>c:\users\kristina\documents\visual studio 2008\projects\s4 ch7 assignment\s4 ch7 assignment\s4 ch7 assignment.cpp(104) : error C2065: 'zeroCount' : undeclared identifier
1>c:\users\kristina\documents\visual studio 2008\projects\s4 ch7 assignment\s4 ch7 assignment\s4 ch7 assignment.cpp(104) : error C2065: 'evenCount' : undeclared identifier
1>c:\users\kristina\documents\visual studio 2008\projects\s4 ch7 assignment\s4 ch7 assignment\s4 ch7 assignment.cpp(104) : error C2065: 'oddCount' : undeclared identifier
1>c:\users\kristina\documents\visual studio 2008\projects\s4 ch7 assignment\s4 ch7 assignment\s4 ch7 assignment.cpp(109) : error C2065: 'outfile' : undeclared identifier
1>c:\users\kristina\documents\visual studio 2008\projects\s4 ch7 assignment\s4 ch7 assignment\s4 ch7 assignment.cpp(113) : error C2065: 'outfile' : undeclared identifier
1>c:\users\kristina\documents\visual studio 2008\projects\s4 ch7 assignment\s4 ch7 assignment\s4 ch7 assignment.cpp(116) : error C2065: 'outfile' : undeclared identifier
1>c:\users\kristina\documents\visual studio 2008\projects\s4 ch7 assignment\s4 ch7 assignment\s4 ch7 assignment.cpp(118) : error C2065: 'outfile' : undeclared identifier