Generic programming.

Code removed.
Last edited on
Your y.cpp code works fine getting the characters from the keyboard.

What doesn't work is:
1
2
3
4
5
6
7
8
9
10
template <typename T> 
double calculateAverage( const T *array, const int count )
{
double average;
for(int i = 0; i < count; i++)
average += array[i];
average /= count;

return average;
}


In this template, average is never initialized. I changed the template to:
1
2
3
4
5
6
7
8
9
10
template <typename T> 
double calculateAverage( const T *array, const int count )
{
double average = 0;
for(int i = 0; i < count; i++)
average += array[i];
average /= count;

return average;
}


and entered characters from the keyboard and got the correct answer.

BTW: The same problem is in x.cpp also.
Thank you very much sir, for the guidance. Yes it works now but I wonder how can such a small proble of not initializing cause the char and double data type not to work properly but int and long would work nicely. What could be the logic behind it?
If you use the same int and long without initializing in vc++ 2005 it will throw you the same issue as you faced for char and double.Because in older compilers it used to assign the int and long by default but latest compilers do not allow using an attribute ,without initialization.
OK Thank you dear.
Topic archived. No new replies allowed.