Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   C compiling problem (http://www.go4expert.com/forums/c-compiling-problem-t6656/)

tyler3777 3Oct2007 06:43

C compiling problem
 
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 ()

DaWei 3Oct2007 10:47

Re: C compiling problem
 
Please read the "Before you make a query" thread and correct your post.

shiftlord 25Jan2008 02:22

Re: C compiling problem
 
Tyler3777

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

BesRegs

Civic 30Jan2008 15:13

Re: C compiling problem
 
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);
}



All times are GMT +5.5. The time now is 14:14.