WizaJ
WizaJ

Reputation: 33

Using operator ^ with `double` operands emits compiler error

I have come up with some code and it does not compile.

Error 1 error C2296: '^' : illegal, left operand has type 'double'
2 IntelliSense: expression must have integral or enum type

The code I was able to come up with is as follows:

#include<iostream>
#include<cmath>
using namespace std;

void getnumbers();
void showmean();
void showdev();

double x1, x2, x3, mean, dev;

void getnumbers()
{
    cout<<"Please enter three numbers\n";
    cin>>x1,x2,x3;
}

void showmean()
{
    mean=(x1+x2+x3)/3;

    cout<<"The mean of"<<x1<<", "<<x2<<", "<<x3<<" is "<<mean<<endl;
}

void showdev()
{
    dev=sqrt((x1 - mean)^2) + (x2 - mean)^2 + (x3 - mean)^2/3;

    cout<<"The standard deviation of"<<x1<<", "<<x2<<", "<<x3<<" is "<<dev<<endl;
}

int main()
{
    getnumbers();
    showmean();
    showdev();

    system("pause");

    return 0;
}

Upvotes: 0

Views: 431

Answers (2)

im so confused
im so confused

Reputation: 2121

  1. You can't take powers like that in C++

    • Use std::pow() or x*x for mathematical x2
  2. For multiple inputs it's cin >> x1 >> x2 ...etc

  3. In your SD equation, your close paren is in the wrong spot I think, mathematically.

    • it should be SD = sqrt( (x*x + y*y) / z ). Your close paren makes it x + y*y/3 (or y2/3, forget precedence)

Upvotes: 6

digitalvision
digitalvision

Reputation: 2622

Apart from the incorrect operator for squaring the values, and the missing parenthesis, the given program doesn't fulfill the part of the assignment that says that the functions should return the calculated values. For example:

double calculate_mean()
{
    return (x1+x2+x3)/3;
}

Upvotes: 2

Related Questions