Hello all, I am working on teaching myself to code and have hit a wall. The top code gives me the sqrt I need (roughly 9.9) to get out of the bottom code. The bottom code by the time it gets were I need it gives a list of usually numbers, but sometimes letters work there way in two. Really new and the lingo can be overwhelming. If someone could point out my error(s) I would be very grateful.
/* Timothy K. Lahey III
C/C++ CCV Brattleboro, VT.
Number set manipulator */
#include "stdafx.h"
#include <cmath>
usingnamespace std;
#include <fstream>
#include <iostream>
#include <cstdlib>
usingnamespace std;
double standDev(double);
int main() {
double setAvg(36);
double setVar(98);
cout << "The average of this set of numbers is... " << setAvg << endl;
cout << "The variance of this set of numbers is... " << setVar << endl;
cout << "The standard deviation of this set of numbers is... " << standDev << endl;
return 0;
}
double standDev(double setVar) {
double deviation = sqrt(setVar);
return deviation;
}
On line 28: cout << "The standard deviation of this set of numbers is... " << standDev << endl;
standDev is not a standard deviation, but rather a function.
To call the function (i.e., to actually compute the standard deviation), you have to follow the function name with parentheses, with function arguments inside.
In this case, standDev is a function of the variance, so put the variance in the parentheses: cout << "The standard deviation of this set of numbers is... " << standDev(setVar) << endl;
What is printed is a pointer to function, which you don't need to care about now, but suffice it to say that it is displayed as a number in hexadecimal (base-16) notation. The 16 digits in that representation include 0-9 and a-f, which is why the letters a-f might appear in the output.
There is no implicit conversion from a pointer to function to a pointer to object; with a conforming implementation, what is displayed would be the result of the conversion to bool (always true, in this case).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
void foo() {}
int main()
{
std::cout << foo << '\n' // 1
<< std::boolalpha << foo << '\n' ; // true
// const void* pv = &foo ; // *** error: no implicit conversion
// conditionally supported (if supported, the result is implementation defined).
// in practice, this conversion via a cast is generally supported
std::cout << reinterpret_cast< constvoid*>(foo) << '\n' ;
}
With the -Za compiler option (demand strict conformance, akin to -std=c++17 -pedantic-errors with the GNU compiler), the Microsoft compiler is conforming in this regard.