Thread: Please help!! Syntax error!!

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    8

    Please help!! Syntax error!!

    I keep getting an error in my for statement. Syntax error before "double". Please help anyone!!!

    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<conio.h>

    double factorial(int num); /*function to compute factorial of a no.*/
    int main()
    {
    int terms;
    int i;
    double eCalc=0; /*to store the calculated value of e*/
    double e=2.718281828458;
    double difference;

    do
    {
    printf("Enter no. of terms (n): ");
    scanf("%d",&terms);
    }
    while(terms<=0); /*continue to loop until a positive value is entered*/


    for(i=0; i<=terms ; i++)
    {
    eCalc+=double(1)/factorial(i); /*using formula e = 1/0! + 1/1! + 1/2! + ... + 1/n!*/
    }

    difference = fabs(e - eCalc);

    printf("\nThe approximate value of e : %.12f",e);
    printf("\nCalculated mathematical constant e : %.12f",eCalc);
    printf("\nThe difference is : %.12f",difference);

    /*the .12 after the % signs specifies that there are 12 decimal*/
    /*places*/

    getch();

    }
    double factorial(int num) /*recursive function to compute factorial*/
    {
    if(num==1||num==0) /*the factorial of zero is also 1, it's a mathematical rule!*/
    {
    return 1;
    }
    /*the control won't reach the following line if the above*/
    /*condition becomes true as the 'return' would also exit*/
    /*the function*/
    return num * factorial(num - 1);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Did you read this?
    << !! Posting Code? Read this First !! >>

    I don't think so, otherwise you would have used code tags.
    That's [code]Your code here[/code]

    Try and "Edit" your post (see icon bottom right of your post) and correct this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<conio.h>
    
    double factorial(int num); /*function to compute factorial of a no.*/
    int main()
    {
    int terms;
    int i;
    double eCalc=0; /*to store the calculated value of e*/
    double e=2.718281828458;
    double difference;
    
    do
    {
    printf("Enter no. of terms (n): ");
    scanf("%d",&terms);
    }
    while(terms<=0); /*continue to loop until a positive value is entered*/
    
    
    for(i=0; i<=terms ; i++)
    {
    eCalc+=double(1)/factorial(i); /*using formula e = 1/0! + 1/1! + 1/2! + ... + 1/n!*/
    }
    
    difference = fabs(e - eCalc);
    
    printf("\nThe approximate value of e : %.12f",e);
    printf("\nCalculated mathematical constant e : %.12f",eCalc);
    printf("\nThe difference is : %.12f",difference);
    
    /*the .12 after the % signs specifies that there are 12 decimal*/
    /*places*/
    
    getch();
    
    }
    double factorial(int num) /*recursive function to compute factorial*/
    {
    if(num==1||num==0) /*the factorial of zero is also 1, it's a mathematical rule!*/
    {	
    return 1;
    }
    /*the control won't reach the following line if the above*/
    /*condition becomes true as the 'return' would also exit*/
    /*the function*/
    return num * factorial(num - 1);
    }
    Last edited by tanik07; 04-07-2011 at 05:51 AM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    How can you program if you can't even read?

    I don't think so, otherwise you would have used code tags.
    That's [ code ]Your code here[ /code ]
    without the spaces.

    Code:
    eCalc+=double(1)/factorial(i); /*using formula e = 1/0! + 1/1! + 1/2! + ... + 1/n!*/
    If you're trying to cast 1 as a double, the proper syntax is

    Code:
    eCalc+=(double)1/factorial(i); /*using formula e = 1/0! + 1/1! + 1/2! + ... + 1/n!*/

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    8
    Thank you!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM