Ok, so I am doing an assignment for my C++ class at a local community college. The assignment is that I have to program the series "sine(x) = (x^1/1!) - (x^3/3!) + (x^5/5!) - (x^7/7!)....". I have coded everything, but no matter what I try, the while loop in my sine function, does not want to work the way I want it to. I have to enter a precision, such as .001 so the user can see how close the program gets. Anyways, the loop never executes properly. Try and see if you can spot the problem, here is my source code..

Code:

#include <iostream>
#include <string>
#include <math.h>
using namespace std;
const double PI = acos(-1);
double d_to_r(double term)
{
double radians;
radians = term * (PI/180);
return radians;
}
long factorial(int x)
{
long product = 1;
int k=0;
while (k<x)
{
k++;
product = product * k;
}
return product;
}
double power(double x, int p)
{
double product = 1;
int k = 0;
while (k<p)
{
k++;
product = product * x;
}
return product;
}
double sine(double x, double precise)
{
double term = 1;
int k = 1;
double sum = 0;
do
{
cout << "k: " << k << endl;
cout << "term: " << term << endl;
cout << "sum: " << sum << endl;
term = (power(-1, k+1) * (power(x, 2*k-1)/factorial(2*k-1)));
sum += term;
k++;
}while ((abs(term)<precise) || (k<7/*Due to factorial, after 7, it is not accurate*/));
return sum;
}
int main()
{
string conversion; //The string for the conversion question
double term; //The double to hold the angle
double precise; // The double to hold precision
cout << "Hi, welcome to the Sine Function\n";
cout << "Please enter a angle for the Sine Function\n";
cin >> term;
cout << "To what precision?\n";
cin >> precise;
cout << "Is the value in degrees or radians?\n";
cout << "If the value is in degrees, type 'degrees', otherwise you\n";
cout << "may type anything else.\n";
cin >> conversion;
if (conversion == "degrees")
{
cout << sine(d_to_r(term), precise);
cout << sin(term);
}
else
{
sine(term, precise);
cout << sin(term); //math.h function to compare
}
return 0;
}

Ok, all help will be appreciated, btw d_to_r converts degrees to radians..