C compiling problem

Discussion in 'C' started by tyler3777, Oct 3, 2007.

  1. tyler3777

    tyler3777 New Member

    Joined:
    Oct 3, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I am new here and started learning C from a tutorial. The tutorial comes with sample codes to learn from but when a compile the following (using gcc on ubuntu) I get an error which I will put after the code. I have tried using gcc, cc, and g++ commands to compile it as I dont really know which is best. Can anyone let me know what is wrong or if there is something wrong with the code. Thanks a lot.

    CODE:
    Code:
           /* Chapter 5 - Program 1 - SUMSQRES.C */
    #include <stdio.h>
    
    int sum; /* This is a global variable */
    
    int main()
    {
    int index;
    
       header();          /* This calls the function named header */
       for (index = 1 ; index <= 7 ; index++)
          square(index);  /* This calls the square function */
       ending();          /* This calls the ending function */
    
       return 0;
    }
    
    
    header()        /* This is the function named header */
    {
       sum = 0;     /* Initialize the variable "sum" */
       printf("This is the header for the square program\n\n");
    }
    
    
    square(number)   /* This is the square function */
    int number;
    {
    int numsq;
    
       numsq = number * number;  /* This produces the square */
       sum += numsq;
       printf("The square of %d is %d\n", number, numsq);
    }
    
    
    ending()   /* This is the ending function */
    {
       printf("\nThe sum of the squares is %d\n", sum);
    }
    
    
    /* Result of execution

    This is the header for the square program

    The square of 1 is 1
    The square of 2 is 4
    The square of 3 is 9
    The square of 4 is 16
    The square of 5 is 25
    The square of 6 is 36
    The square of 7 is 49

    The sum of the squares is 140

    */

    ERROR:
    tyler@ubuntu:~/Desktop/c$ g++ -o SUMSQRES SUMSQRES.C
    SUMSQRES.C: In function ‘int main()’:
    SUMSQRES.C:10: error: ‘header’ was not declared in this scope
    SUMSQRES.C:12: error: ‘square’ was not declared in this scope
    SUMSQRES.C:13: error: ‘ending’ was not declared in this scope
    SUMSQRES.C: At global scope:
    SUMSQRES.C:19: error: ISO C++ forbids declaration of ‘header’ with no type
    SUMSQRES.C:26: error: expected constructor, destructor, or type conversion before ‘(’ token
    SUMSQRES.C:28: error: expected unqualified-id before ‘{’ token
    SUMSQRES.C:37: error: ISO C++ forbids declaration of ‘ending’ with no type\

    Note: the †things are supposed to be ()
     
    Last edited by a moderator: Oct 3, 2007
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Please read the "Before you make a query" thread and correct your post.
     
  3. shiftlord

    shiftlord New Member

    Joined:
    Jan 24, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Tyler3777

    Did you ever get an answer to this post??? If not look for a file canswers.htm

    BesRegs
     
  4. Civic

    Civic New Member

    Joined:
    Jan 29, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student
    Location:
    Lithuania
    Home Page:
    http://cooldj18.googlepages.com
    At first - I an non english speaker, so I might make some mistakes. And secondly - this program should be working now fine:
    Code:
           /* Chapter 5 - Program 1 - SUMSQRES.C */
    #include <stdio.h>
    
    int sum; /* This is a global variable */
    
    void header (void);  // You should write function headers if you use it before defining
    void square (int);    // I guess you programmed in Pascal before
    void ending (void); // Here, in C, you must always define the return type of a function
    
    int main()
    {
    int index;
    
       header();          /* This calls the function named header */
       for (index = 1 ; index <= 7 ; index++)
          square(index);  /* This calls the square function */
       ending();          /* This calls the ending function */
    
       return 0;
    }
    
    
    void header()        /* This is the function named header */
    {
       sum = 0;     /* Initialize the variable "sum" */
       printf("This is the header for the square program\n\n");
    }
    
    
    void square(int number)   /* This is the square function */
    //int number;    This line was not correct I think.
    {
    int numsq;
    
       numsq = number * number;  /* This produces the square */
       sum += numsq;
       printf("The square of %d is %d\n", number, numsq);
    }
    
    
    void ending()   /* This is the ending function */
    {
       printf("\nThe sum of the squares is %d\n", sum);
    }
    
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice