Error: 'name' was not declared in tis scope

2

I have doubts with this task to be programmed in php but in c ++ I am just learning, The program marks me an error in the function

  

xalan (x, i)    15 24 C: \ Users \ admin \ Documents \ task8 \ 9 \ main.cpp [Error] 'xalan' was not declared in this scope

#include <iostream>
#include <string>
#include <stdio.h>
//sin(x)=x-x^3/(3!)+x^5/(5!)+...+(-1)^n(x^(2n+1))/((2n+1)!)

int main(int argc, char** argv) {
    float x,sinx,sinx2;
    printf("\nprograma para calcular sin(x)");
    printf("\nintrodusca el angulo x en radianes \n");
    scanf("%f",&x);
    sinx=x-(x*x*x)/(3*2)+(x*x*x*x*x)/(5*4*3*2)-(x*x*x*x*x*x*x)/(7*6*5*4*3*2)+(x*x*x*x*x*x*x*x*x)/(9*8*7*6*5*4*3*2);
    printf("\nsin(x)=%f ",sinx);
    sinx2=x;
    for(int i=1;i<10;i++)
        sinx2=sinx2+xalan(x,i);
    printf("\nsin2(x)=%f ",sinx2);
}


float xalan(float x,int n)
{   //(-1)^n(x^(2n+1))/((2n+1)!)
    int fact=2*n+1;
    float xn=1;
    for(int i=(2*n);i>0;i--)
        fact=fact*(i);
    for(int i=1;i<=n;i++)
    xn=xn*(-1)*(x*x);
    xn=xn*x;
    return xn/(fact);
}
    
asked by Carlos Enrique Gil Gil 04.12.2018 в 02:49
source

2 answers

5

Upload the declaration of the xalan function to before main.

#include <iostream>
#include <string>
#include <stdio.h>

float xalan(float x,int n)
{ 
   ...
}

int main(int argc, char** argv) {
   ...
}

The problem is that xalan has not been loaded by the program when you're trying to call it.

    
answered by 04.12.2018 / 03:03
source
1

You need the prototype of the function, this is declared before the main as follows:

float xalan(float x,int n);

Prototypes are required in c ++ if the functions are to be declared below the main. If they are going to be done before the main, a prototype is not necessary.

    
answered by 05.12.2018 в 22:37