2013-09-16, 12:27 AM
In my very first "computer programming" class, 1980 in highschool, the teacher started by saying "Calculate 2+3".
Several people yelled out "five!"
The teacher looked at them quizzically and said, "I didn't ask you to tell me what it adds up to."
The variable "valueCount" is local. You declared it inside the function, which means no other bit of code can see it. This is called "scope", and the rule of thumb is: a variable can only be seen/accessed inside the closest { } pair it's declared in.
In order to get "valueCount" out to main() so it can print it, you must use the "return" statement when your function is ready to complete.
So, InputCount will look like this:
And the output statement in main() will look like this:
I hope that helps.
Several people yelled out "five!"
The teacher looked at them quizzically and said, "I didn't ask you to tell me what it adds up to."
The variable "valueCount" is local. You declared it inside the function, which means no other bit of code can see it. This is called "scope", and the rule of thumb is: a variable can only be seen/accessed inside the closest { } pair it's declared in.
In order to get "valueCount" out to main() so it can print it, you must use the "return" statement when your function is ready to complete.
So, InputCount will look like this:
Code:
int InputCount (ifstream &inF)
{
int valueCount = 0; //Initialize the value count at 0.
while (inF >> value)
{
valueCount += 1; //Increment value count.
}
return valueCount;
}And the output statement in main() will look like this:
Code:
[strike] int InputCount (&inF);[/strike] This is not needed, the function was already declared above.
//Print results
cout << InputCount(inF) << " VALUES IN FILE" << endl;I hope that helps.

