Thread: expected expression before 'double' (sum problem with functions)

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    4

    expected expression before 'double' (sum problem with functions)

    I'm a beginner with programming in C and I have a problem to solve at which I get stucked.
    So my program reads positive numbers until the value 0 then it calculates the sum of numbers. Then I have to write a function which displays the sum. Also, after that I need to enterr a number "y" from keyboard and I need to find the result of the sum/y.
    My program needs to be built with functions. I received the functions name with parameters and I built them like this:
    Code:
    
    
    Code:
    #include <stdio.h> 
    
    
    // Shows a message with what the program is doing.
    void ShowIntroduction(void)
    {
        printf("My program finds a sum etc");
    }
    
    
    // find the sum of the numbers enter until 0 value
    int sum(void)
    {
        int s=0,n;
        do
        {
            scanf("%d",&n);
            if (n > 0)  
                s=s+n;
        }
        while(n != 0);
    
    
       return s;  
    }
    
    
    // show the result
    void sumResult(int a)
    {
        printf("The sum is %d", a);
    }
    
    // find the result of sum/y
    double result(int s,int y)
    {
            double res;
            res=double(s)/double(y);
            return res;
    }
    
    int main()
    {
        int y;
        scanf("%d",&y);
        ShowIntroduction();
        sumResult(sum());
        result(sum(),y);
    
        return 0;
    }


    The problem is that I get the error: "expected expression before 'double' "
    Where is my mistake?


  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,675
    Code:
    res=double(s)/double(y);
    The above is not valid C code.
    Lookup how to do C casting.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    4
    Thanks!
    I solved the problem.Now, I got another one.
    The program shows the sum result then it asks me to introduce the values again until the value 0 then the program closes.
    Like in the picture below:
    expected expression before 'double' (sum problem with functions)-pc-png
    The program should display the result of 10/2 in this case but I don't know why is not working.

  4. #4
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    29
    That's clear,
    because yo call the function 'sum' first in line 47 as the first argument of function 'sumResult'
    and then in line 48 again. The variable 'n' is only valid in 'sum', after calling 'sum' again, the
    last value is away. By the way you should insert a line to show the quotient of '(doublel)sum/(double)y'
    and one who shows what is to put in by variable 'y'
    May be it is also better to set first the quantity, how much values you want to read in.
    If you want for example to calculate the average temperatures in Celsius or in Farenheit it would be
    possible that you can get an Value of Zero degree. But in the example above you can't calculate
    the average, you can't save a value of Zero.

    Code:
    #include <stdio.h> 
     
     
    // Shows a message with what the program is doing.
    void ShowIntroduction(void)
    {
        printf("My program finds a sum etc\n");
    }
     
     
    // find the sum of the numbers enter until 0 value
    int getsum(int *s)
    {
        int n;
        
        *s = 0;
        do
        {
         printf("Enter a integer: ");
         scanf("%d", &n);
            if (n > 0)  
                *s += n;
        }
        while(n != 0);
     
     
       return *s;  
    }
     
     
    // show the result
    void sumResult(int a)
    {
        printf("The sum is %d\n", a);
    }
     
    // find the result of sum/y
    double result(int s,int y)
    {
            double res;
            res=double(s)/double(y);
            return res;
    }
     
    int main()
    {
        int y, sum;
      
        
        ShowIntroduction();
        
        printf("Enter divisor: ");
        scanf("%d",&y);    
        
        sumResult(getsum(&sum));
        printf("quotient: %lf\n", result(sum,y));
     
        return 0;
    }

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,458
    What a crappy way to structure a program (just my opinion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected constant expression?
    By GroogFish in forum C Programming
    Replies: 3
    Last Post: 05-08-2012, 12:17 AM
  2. Replies: 11
    Last Post: 04-15-2012, 07:10 PM
  3. new to C, expected expression before 'double' message
    By jadejones in forum C Programming
    Replies: 3
    Last Post: 01-02-2012, 10:43 AM
  4. error: expected an expression
    By cdmstr in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2011, 02:00 PM
  5. expected primary expression
    By lilhawk2892 in forum C++ Programming
    Replies: 10
    Last Post: 11-22-2007, 07:50 PM

Tags for this Thread