Victor Martins Victor Martins - 29 days ago 15
C++ Question

Why is my equation giving Error must have an integral expression or unscoped enun type?

so hey guys, I thought to make a simple program to learn about functions and I ran across this error I never had before could someone help me?

#include<iostream>
#include<iomanip>
#include<string>


using namespace std;

float SphereVolume(float r);

int main() {

/*

Calculating a sphere.

*/
float radius;
cout << "Sphere Calc....\n";
cout << "\tPlease Enter Radius of Sphere: ";
cin << radius;

SphereVolume(radius);

return 0;
}


float SphereVolume(float r) {

double const PI = 3.14159;
float volume;

volume = 4/3 * PI * r ^ 3; // error starts here.

return volume;
}


I cant seem to understand why this is happening, the error, start when I try to declare the volume equation and it says the error??

Answer
volume = 4/3 * PI * r ^ 3; // error starts here.

4 / 3 is integer division -> 1  

r ^ 3  is r xor-ed with the integer 3. 

You can't xor a float. If you want to cube a float, the simplest solution is to multiply it three times.

Comments