I have to lines listed.. a cin >> x and a scanf("%f",&x). If I use cin program works perfectly.. change it to scanf and values arent' even corect any longer.. different decimal values all together.. anyone???

#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

double ApproxCos(double,double);

int main()
{
	double error = 10e-6;  //varible to change error value
	double x;
	printf("Input x to approximate cos: ");
	scanf("%f",&x);  // this DOES NOT WORK
	//cin >> x;  // this line works
	printf ("cos =  %f\n", cos(x));
	
	return 0;
}

double ApproxCos(double x, double error)
{
	double sum=0;  
	double factorial = 1;
	for (int n=0; abs(cos(x) - sum) > error; ++n)  
	{
		if (n > 0)  // only change value of factorial after first loop
			factorial *= (2*n) * (2*n-1); //recursively calculate factorial
		sum += pow(-1.0,n) * pow(x,n*2) / factorial; // summation of equation
		
	}
	return sum;
}

The specifier for double with scanf is %lf. %f is for float. Also note that we have a separate forum for C++ questions. This is the C forum.

The specifier for double with scanf is %lf. %f is for float. Also note that we have a separate forum for C++ questions. This is the C forum.

this program is meant to be specifically C. I had to use a C++ cin to make it work...

The specifier for double with scanf is %lf. %f is for float. Also note that we have a separate forum for C++ questions. This is the C forum.

%lf fixed it.. duh my stupid mistake.. thanks for the help .. :)

>this program is meant to be specifically C. I had to use a C++ cin to make it work...
That's ominous. Just make sure you're actually compiling as C, otherwise you might encounter some confusing areas where C and C++ don't see eye to eye.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.