How do I solve this cmath compiler error?

344 views Asked by At

Im fairly new to coding. I am doing a project with function calls, and my POW() and SQRT() functions say they're not declared in this scope. I'm not particularly sure how to solve this compiler error and appreciate any feedback

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

double average(double a,double b, double c, double d);
void stdev(double a, double b, double c, double d);
int main(){
    double num1,num2,num3,num4;
    char repeat;
    do{
    cout << "Enter four decimal numbers: ";
    cin >> num1 >> num2 >> num3 >> num4;
    average(num1, num2, num3, num4);
    stdev(num1,num2,num3,num4);
    
    cout << "Please enter y to continue: ";
    cin >> repeat;
    cout << endl;
    }
    while (repeat == 'y');
    return 0;
}
double average(double a, double b, double c, double d){
    double ave;
    ave = (a+b+c+d)/4;
    return ave;
}
void stdev(double a, double b, double c, double d, double average){
    double std, ave;
    std = SQRT(POW(a-ave,2)+POW(b-ave,2)+POW(c-ave,2)+POW(d-ave,2));
    cout << "The average = "<< ave << endl;
    cout << "The standard deviation of these numbers = "<<std<<endl;
    return;
    }

1

There are 1 answers

0
w0lf On

sqrt and pow must be in lowercase and you also missed average on stdev function definition. https://www.cplusplus.com/reference/cmath/

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

double average(double a, double b, double c, double d);
void stdev(double a, double b, double c, double d, double average);

int main() {
    double num1, num2, num3, num4;
    char repeat;
    do {
        cout << "Enter four decimal numbers: ";
        cin >> num1 >> num2 >> num3 >> num4;
        double avg = average(num1, num2, num3, num4);
        stdev(num1, num2, num3, num4, avg);

        cout << "Please enter y to continue: ";
        cin >> repeat;
        cout << endl;
    } while (repeat == 'y');
    return 0;
}

double average(double a, double b, double c, double d) {
    double ave;
    ave = (a + b + c + d) / 4;
    return ave;
}
void stdev(double a, double b, double c, double d, double average) {
    double std = sqrt(pow(a - average, 2) + pow(b - average, 2) + pow(c - average, 2) + pow(d - average, 2));
    cout << "The average = " << average << endl;
    cout << "The standard deviation of these numbers = " << std << endl;
    return;
}