Cannot Figure Out Why I Get Parse Error Before ','

This is a discussion on Cannot Figure Out Why I Get Parse Error Before ',' within the C Programming forums, part of the General Programming Boards category; Hello cprogramming.com, I am currently taking my first c programming course as a requirement for my mechanical engineering degree, so ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Cannot Figure Out Why I Get Parse Error Before ','

    Hello cprogramming.com, I am currently taking my first c programming course as a requirement for my mechanical engineering degree, so please bare with me if i am so oblivious. I am having the hardest time figuring out why i keep getting this parse error.

    prompt:

    Write a function that accepts two arguments, integer n and real number x, computes the following series, and returns the computed result:

    1 2(x^2)/1! + 3(x^4)/2! 4(x^6)/3! + + [(-1)^n](n+1)(x^2n)/n!

    The general term is:

    negative one to the power of n
    multiply by (n plus one)
    multiply by x to the power of (2n)
    divided by n factorial


    Then write a C program that request two input from the user:
    -- an integer number, n
    -- a real number, x;

    call the function passing those two input as arguments,
    output the result returned by the function.


    OK, so this is as far as i got. Everything LOOKS right.. but am i missing something so obvious??

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    int main()
    {
        int x, n;
    
        printf("Enter a value for n.\n");
        scanf("%d", &n);
        printf("n = %d\n", n);
        printf("Enter a value for x.\n");
        scanf("%d", &x);
        printf("x = %d\n", x);
    
        printf("Result = %d\n", compute(int x , int n));
    
        system("pause");
        return 0;
    }
    
    int compute(int x, int n)
    {
        int i, term, xsq, res;
        i = 1;
        xsq = x*x;
        term = -1;
    
        while(i<=n)
        {
            term = term*(-1)*xsq/i;
            i = i+1;
        }
    
    res=term*(n+1);
    
    return res;
    }
    now when i compile it tells me i have a parse error before ',' in the line that i bolded. can anyone please tell me where i went wrong? Thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Did you type
    Code:
    printf(char * "Enter a value for n.\n");
    or
    Code:
    scanf(char *"%d", int * &n);
    ? Then why would you type
    Code:
    compute(int x , int n)
    ?

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,458
    when you call function you do it like this:
    Code:
    compute (2,3);
    or like this
    Code:
    int x =2;
    int n=3;
    compute(x,n);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    i originally had it down as compute (x,n); but i got a different error that states implicit declaration of function 'int compute(...)', which made it seem like it was worse off..

    vart, in my main function, i set the x and n values to be an input from the user so there cannot be any preset values, would there be a different syntax for that? i still do not know why its resulting in the error

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to have a prototype of a function before you can use it (that says "hey this is a function, and it takes these kinds of things, and it returns this sort of a guy".) In this case the prototype is
    Code:
    int compute(int x, int n);
    and must appear before it is used.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    friggin a, duh i cant believe i missed that.. thanks a ton fellas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. * Character in
    By Jonnyb42 in forum C Programming
    Replies: 14
    Last Post: 01-15-2010, 09:28 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21