Illegal exponent operator, how do i fix this?

• 09-21-2001
Unregistered
Illegal exponent operator, how do i fix this?

Bug: It says my exponent operator is incorrect, how do i fix this? Thanks

#include <iostream>
using namespace std;

//Math functions
#include <cmath>

//Function prototype compute_range()
double compute_range(double angle, double velocity);

//Function prototype show_results()
void show_results(double range);

int main()
{

//boolean condition set to false
bool cont = false;

//Intialized variables
double angle, velocity, range;

//do while loop
do{

cout << "Please input an angle in degrees: ";
cin >> angle;
cout << endl;
cout << "Please input velocity in meters per second: ";
cin >> velocity;
cout << endl;

range = compute_range(angle, velocity);

show_results(range);

cout << "Continue (y/n) ? ";

//End while
}while(cont);

return 0;
}
/************************************************** */
//Function call computer_range()
double compute_range(double angle, double velocity){

double const GRAVITY = 9.8;
double range;

range = sin(2 * angle) * velocity^2 / GRAVITY;

return(range);
}
/************************************************** */

/*************************************/
//Function call show_results()
void show_results(double range){

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
cout << "The range is: " << range;

return;
}
/*************************************/
• 09-21-2001
DavidP
the ^ is not the exponent operator in C++. Use the pow() function. In this case, you would do: pow(velocity, 2). It is in math.h.

If you wanted to make the ^ and exponent operator, you would have to overload it, like this:

double operator ^ (double lhs, double rhs)
{
return pow(lhs, rhs);
}

THEN you could say velocity^2