C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Confused about functions! Please help Posted by uoneluckybug on 17 Mar 2009 at 6:10 AM
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
Report
Re: Confused about functions! Please help Posted by psychofox19 on 17 Mar 2009 at 7:18 AM
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
Report
Re: Confused about functions! Please help Posted by uoneluckybug on 17 Mar 2009 at 7:41 AM
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.
Report
Re: Confused about functions! Please help Posted by psychofox19 on 17 Mar 2009 at 7:44 AM
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.
Report
Re: Confused about functions! Please help Posted by uoneluckybug on 17 Mar 2009 at 7:52 AM
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 <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); Line 54???

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

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; //Line 98???
}

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

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

outfile << "The number of odd numbers is: " << oddCount //Line 113???
<< endl;

outfile << "The sum of the numbers is: " << sum << endl; //Line 116???

outfile << "The average of the numbers is: " << average << endl; //Line 118???
} //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
Report
Re: Confused about functions! Please help Posted by psychofox19 on 17 Mar 2009 at 6:45 PM
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.
Report
Re: Confused about functions! Please help Posted by uoneluckybug on 18 Mar 2009 at 2:49 AM
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!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.