sqrt issues

Jul 25, 2018 at 2:14am
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "stdafx.h"
#include <cmath>
using namespace std;
#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;




int main() {

	double setVar = 98;
	double deviation = sqrt(setVar);
	cout << deviation << endl;


	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*	Timothy K. Lahey III
C/C++ CCV Brattleboro, VT.
Number set manipulator */

#include "stdafx.h"
#include <cmath>
using namespace std;
#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace 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;
}
Jul 25, 2018 at 3:35am
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;

See your textbook on functions, or the tutorial on this site.
http://www.cplusplus.com/doc/tutorial/functions/

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.
Last edited on Jul 25, 2018 at 5:01am
Jul 25, 2018 at 4:04am
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< const void*>(foo) << '\n' ;
}

http://coliru.stacked-crooked.com/a/0f83f4092afb7904
Jul 25, 2018 at 4:58am
There I go again, making assumptions about the language from the problem description. Thank you.

I'm quite certain you're right, but MSVC (which OP is using, see "stdafx.h") seems to do as I remembered:
http://rextester.com/BBF18325
Last edited on Jul 25, 2018 at 6:08am
Jul 25, 2018 at 6:42am
This is a Microsoft extension.

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.

http://rextester.com/ZGMB1185
Jul 25, 2018 at 1:59pm
The amount of time I spent on that is now embarrassing, considering the fix was that small. Thank you very much for the help!
Topic archived. No new replies allowed.