What does my error mean?

Hi, I'm new to C++ and this website. I have come here before for help with my coding but I could not find the answer I needed for my current problem. I am receiving the error "illegal, left operand has type "double" and "illegal, right operand type has "double" in the line where the standard_dev equation is displayed.

I have seen this error fixed before on this site but the code was too complicated for me to understand why it occurred and how it was fixed.

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
using namespace std;
void my_statistics(double x1, double x2, double x3, double x4);
void output(double average, double standard_dev);

double x1, x2, x3, x4, average, standard_dev;

int main()
{

	cout.setf(ios::fixed);
	cout.precision(4);

	cout<<" Input the values for x1 through x4 "<<endl;
	cin>>x1>>x2>>x3>>x4;
	my_statistics(x1, x2, x3, x4);
	output(average, standard_dev);

return 0;
}
////////////////////////////////////////////////////
void my_statistics(double x1, double x2, double x3, double x4, double average, double standard_dev)
	{
		average = 0;
		standard_dev = 0;

average = (x1 + x2 + x3 + x4) / 4;
standard_dev = sqrt(((x1 - average) ^ 2 + (x2 - average) ^ 2 + (x3 - average) ^ 2 +(x4 - average) ^ 2) / 3);
	}


If possible I would like the type to remain as double. As a bonus question, would I use a loop or if statement if I wanted to make the code tell the user to input numbers until they type "n" (at which point the code would move on)?

Thanks for your help.
Could you be more specific about which line the error is on? Also, you need to copy and paste the exact text of the error - never paraphrase.

Sean TMD wrote:
As a bonus question, would I use a loop or if statement if I wanted to make the code tell the user to input numbers until they type "n" (at which point the code would move on)?
1
2
3
4
5
6
7
8
9
10
11
std::cout << "Enter numbers, then enter 'n' to continue." << std::endl;
std::string input;
while(std::cin >> input && input != "n")
{
    int n;
    if(std::istringstream(input) >> n)
    {
        //do something with n, e.g.:
        my_vector.push_back(n);
    }
}
Last edited on
Yes, sorry about that. The error is occuring in line 27 and the error is:

1>c:\users\user\documents\visual studio 2008\projects\standard dev\standard dev\hw.cpp(34) : error C2296: '^' : illegal, left operand has type 'double'
1>c:\users\user\documents\visual studio 2008\projects\standard dev\standard dev\hw.cpp(34) : error C2297: '^' : illegal, right operand has type 'double'

Also thank you for your response.
I edited my post with an answer to your bonus question.

As for the error you are getting, it is because you are using the XOR operator (the ^ operator is not exponentiation, but instead bitwise XOR). You need to #include <cmath> and use std::pow instead.
Ahhh, thank you so much. That didn't even occur to me. I still have a lot to learn. Thanks again. The code worked after your suggestions.
Topic archived. No new replies allowed.