can anyone fix this to work?
#include<iostream>
#include<cstring>
#include<cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
#define _USE_MATH_DEFINES
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
double recursiveCos(double x);
int main (void)
{
double tmp = 0.0;
double radianAngle = 0.0;
double result = 0.0;
char again = 'Y';
do
{
cout << "Input an angle in radians: ";
cin >> tmp;
while(!cin)
{
cout << "Input an angle in radians: ";
cin.clear();
cin.ignore(255,'\n');
cin >> tmp;
}
try
{
radianAngle = fmod( tmp, 2 * M_PI );
cout << fixed << showpoint << setprecision(3) << tmp << "as a first period angle is " << setprecision(3) << radianAngle << " radians" << endl;
}
catch (exception e)
{
cout << "Exception:" << e.what() << endl;
cout <<"Press the \"Enter\" key to continue";
cin.get();
return EXIT_FAILURE;
}
cout << endl;
cout << "cos<" << tmp << setprecision(3) << ">" << " = " << recursiveCos(result) << endl;
cout << "cosine<" << radianAngle << setprecision(3) << ">" << " = " << recursiveCos(result) << endl;
cout << "Calculate another cosine? (Y/N): Y\b";
if((again = toupper (cin.get()))== '\n')
again = 'Y';
else
cin.ignore(256,'\n'); // read past CR
}
while(again == 'Y' || again == 'y');
cin.get(); // hold window open
return EXIT_SUCCESS;
}
double recursiveCos(double x)
{
return recursiveCos(x-1); //cos(x)
//pow((float)(-1),(float)n)
}
You don't have a terminating condition in your recursion. It's infinite.
The problem lies with your function recursiveCos. It is coded to repeatedly call itself with a smaller and smaller argument. It does and mean nothing.