Combining Functions

  • Thread starter Thread starter Nutkin
  • Start date Start date
N

Nutkin

Hi i have to program a code to perform 1 of 5 functions at the users
request, I have got to the part where i have to program the equations
and i cant seem to get them to link together. Basicly when the used
enters his radians i was to link to a sin x function to calculate the
sin of the number also there will be a factorial function and a cos
function. but none of them are linking.

#include <iostream>

using namespace std;

int factorial()
{
int i=0;
for (i=0; i>0; i--)


return 0;
}




double sin()
{
double sin=0;
double x=0;

sin=x-(x*x*x/(3*2*1))+(x*x*x*x*x/(5*4*3*2*1));

return sin;
}

int main()
{
int item=0;
double x=0;
double pi=3.14159265;

cout <<"Please enter your x value in radians : ";
cin >> x;

cout <<"Select the Equation to Perform:";
cout <<"\n";
cout <<"1 = sin(x)\n";
cout <<"2 = cos(x)\n";
cout <<"3 = tan(x)\n";
cout <<"4 = sin^2(x) + cos^2(x)\n";
cout <<"5 = 2sin(x)cos(x)\n";
cout <<"Enter the Number: ";
cin >> item;

switch (item){
case 1:
cout <<sin();
cout <<"\n\n\n";
break;
case 2:
cout << "Case 2\n\n";
break;

case 3:
cout << "case 3\n\n";
break;
case 4:
cout << "case 4\n\n";
break;
case 5:
cout << "case 5\n\n";
break;
default:
cout <<"No Number Entered";
break;
}


return 0;
}
 
Hi i have to program a code to perform 1 of 5 functions at the users
request, I have got to the part where i have to program the equations
and i cant seem to get them to link together. Basicly when the used
enters his radians i was to link to a sin x function to calculate the
sin of the number also there will be a factorial function and a cos
function. but none of them are linking.

Hi,
It would be helpful if you include the actual error messages.
We are not yet clairvoyant.

If you do math you should also include math.h.
Look in MSDN to see which functions are contained in which library.
There should be no need to program sin and cos functions yourself.

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Nutkin said:
Hi i have to program a code to perform 1 of 5 functions at the users
request, I have got to the part where i have to program the equations
and i cant seem to get them to link together. Basicly when the used
enters his radians i was to link to a sin x function to calculate the
sin of the number also there will be a factorial function and a cos
function. but none of them are linking.

You should have asked your professor first, he/she is paid to help you.
#include <iostream>

using namespace std;

int factorial()
{
int i=0;
for (i=0; i>0; i--)


return 0;
}




double sin()
{
double sin=0;
double x=0;

Local variable x, always zero
sin=x-(x*x*x/(3*2*1))+(x*x*x*x*x/(5*4*3*2*1));

Reusing the name "sin", bad idea.

x = 0, hence sin = 0
return sin;

always 0
}

int main()
{
int item=0;
double x=0;

Another local variable x, distinct from the one in sin().
double pi=3.14159265;

cout <<"Please enter your x value in radians : ";
cin >> x;

This x changes.
cout <<"Select the Equation to Perform:";
cout <<"\n";
cout <<"1 = sin(x)\n";
cout <<"2 = cos(x)\n";
cout <<"3 = tan(x)\n";
cout <<"4 = sin^2(x) + cos^2(x)\n";
cout <<"5 = 2sin(x)cos(x)\n";
cout <<"Enter the Number: ";
cin >> item;

switch (item){
case 1:
cout <<sin();

This always prints 0.
 
Nutkin said:
Hi i have to program a code to perform 1 of 5 functions at the users
request, I have got to the part where i have to program the equations
and i cant seem to get them to link together. Basicly when the used
enters his radians i was to link to a sin x function to calculate the
sin of the number also there will be a factorial function and a cos
function. but none of them are linking.

[snip]

Well, for starters you should be passing the value of x to the functions
as an an argument. The x in your main() is a local variable, so there is
no way the functions can see it (and global variables in C++ are bad
anyway).

What do you mean by "not linking" ?

David Wilkinson
 
Back
Top