#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Posts: 4Member
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.
[b]
CODE:[/b]
#include
#include
#include

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

[b]ERRORS:[/b]

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(54) : error C2065: 'sum' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(74) : error C2065: 'infile' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(75) : error C2065: 'outfile' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(98) : error C2659: '=' : function as left operand

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(104) : error C2065: 'zeroCount' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(104) : error C2065: 'evenCount' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(104) : error C2065: 'oddCount' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(109) : error C2065: 'outfile' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(113) : error C2065: 'outfile' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(116) : error C2065: 'outfile' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(118) : error C2065: 'outfile' : undeclared identifier

• Posts: 41Member
It might help if you mark which lines are which from the errors, so put a comment line after each one it referens i.e. // Line 54
• Posts: 4Member
I agree with you except I stil haven't figured out myself completely how to count out the lines to find out which error is for which line. My count never seems to agree with the error line number. Any help on clarifying this would be appreciated as well. Thank you.
• Posts: 41Member
Open up the file in notepad and go to View then select Status Bar, now evey time you click on a line you can see it's line number on the bar below. That's if you haven't got a compiler with a similar feature anyway.
• Posts: 4Member
OK, I tried to guess which errors went with which lines, that is why they have ? marks after them, because I am not sure. The code is re-posted below with the line error comment guesses.

CODE:
#include
#include
#include

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); [color=Green]Line 54???[/color]

//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; [color=Green]//Line 74???[/color]
outfile << setw(10) << num << " "; [color=Green]//Line 75???[/color]

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; [color=Green]//Line 98??? [/color]
}

void average(int sumCount)
{
int average;
average = sum /(zeroCount + evenCount + oddCount); [color=Green]//Line 98???[/color]
}

void printResults(int zeroCount, int oddCount, int evenCount, int sum, int average) [color=Green]//Line 104???[/color]
{
outfile << "There are " << evenCount << " evens, " [color=Green]// Line 109???[/color]
<< "which includes " << zeroCount << " zeros"
<< endl;

outfile << "The number of odd numbers is: " << oddCount [color=Green]//Line 113???[/color]
<< endl;

outfile << "The sum of the numbers is: " << sum << endl; [color=Green]//Line 116???[/color]

outfile << "The average of the numbers is: " << average << endl; [color=Green] //Line 118???[/color]
} //end printResults

[b]ERRORS:[/b]

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(54) : error C2065: 'sum' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(74) : error C2065: 'infile' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(75) : error C2065: 'outfile' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(98) : error C2659: '=' : function as left operand

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(104) : error C2065: 'zeroCount' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(104) : error C2065: 'evenCount' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(104) : error C2065: 'oddCount' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(109) : error C2065: 'outfile' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(113) : error C2065: 'outfile' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(116) : error C2065: 'outfile' : undeclared identifier

1>c:userskristinadocumentsvisual studio 2008projectss4 ch7 assignments4 ch7 assignments4 ch7 assignment.cpp(118) : error C2065: 'outfile' : undeclared identifier
• Posts: 41Member
Okay you've got a lot of work to do, firstly 'sum' hasn't been declared in main(). Secondly there are various other local variables that haven't been declared in other methods.

For example in getNumber() you need to either redeclare inFile and outFile like you did in main() or pass them through from main() into getNumber() as parameters.

Secondly outfile should be outFile and infile should be inFile in getNumber, C++ is another language that is picky about case when dealing with variables.

Thirdly you've declared a template of average() twice, I think you were meant to write the 2nd as 'sumNumbers(int zeros, int odds, int evens);' and rename the method you already have called sum() to sumNumbers as it doesn't like variables with the same name as a function, otherwise it might think you want to use recursion (a function calling itself over and over).

You'll also need to declare 'sum' again in sumNumbers() as it's still only a local variable not connected to the 'sum' in main() so consider passing the value back in the function return or making it a global variable (declaring a variable outside of any function, including main() will make it accessible by all functions and editable by all too).

Consider doing this for the other variables like average (which hasn't been declared either) and evenCount, oddCount, zeroCount etc.

It's a lot of work but it's needed to sort this mess out.
• Posts: 4Member
Thank you very much for your help!!! I am completely confused about the scope of variables. I was getting so confused with all of the parameter and variable names. I am workin gon fixing that now and then I will see if anything works. Thanks again!