Thread: qUEstion about erros on compiling

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    99

    qUEstion about erros on compiling

    I have the following code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int multiply(long x, long y);
    int divide(long x, long y);
    int add(long x, long y);
    int subtract(long x, long y);
    
    long x;
    long y;
    
    int main()
    {
    char temp[900];
    
    printf("Enter a number:\n");
    fgets(temp, sizeof temp, stdin);
    x=atoi(temp);
    
    printf("Enter another number:\n");
    fgets(temp, sizeof temp, stdin);
    y=atoi(temp);
    
    multiply(x,y);
    divide(x,y);
    add(x,y);
    substract(x,y);
    }
    
    int multiply(long x, long y)
    {
    int answer;
    answer=x*y;
    printf("i times %i is %i\n", x, y, answer);
    }
    
    int divide(long x, long y)
    {
    int answer;
    answer=x/y;
    printf("%i divided by%i is %i\n", x, y, answer);
    }
    
    int add(long x, long y);
    {
    int answer;
    answer=x+y;
    printf("%i plus %i is %i\n", x, y, answer);
    }
    
    int subtract(long x, long y)
    {
    int answer;
    answer=x-y;
    printf("%i minus %i is %i\n", x, y, answer);
    }
    I got this code from a c tutor but when try compile i get:
    "dokimi10.c", line 4: syntax error at or near type word "long"
    "dokimi10.c", line 5: syntax error at or near type word "long"
    "dokimi10.c", line 6: syntax error at or near type word "long"
    "dokimi10.c", line 7: syntax error at or near type word "long"
    "dokimi10.c", line 30: syntax error at or near type word "long"
    "dokimi10.c", line 37: syntax error at or near type word "long"
    "dokimi10.c", line 44: syntax error at or near type word "long"
    "dokimi10.c", line 47: illegal initialization
    "dokimi10.c", line 47: cannot recover from earlier errors: goodbye!
    please tell why do i got these erroes,
    should i add ";" at int "work"(long x, long y)??

  2. #2
    meow nbk's Avatar
    Join Date
    Jul 2004
    Posts
    45
    Code:
    ]int add(long x, long y); // remove semi-colon
    {
    Code:
    multiply(x,y);
    divide(x,y);
    add(x,y);
    substract(x,y);  //spelled wrong
    And with that, I compiled fine on Dev-C++.
    Last edited by nbk; 07-29-2004 at 09:53 PM.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    I doin't get the romove semi colon?! what is a semi colon? that "[" i don't see it?

    then i kepp getting
    "dokimi10.c", line 4: syntax error at or near type word "long"
    "dokimi10.c", line 5: syntax error at or near type word "long"
    "dokimi10.c", line 6: syntax error at or near type word "long"
    "dokimi10.c", line 7: syntax error at or near type word "long"
    "dokimi10.c", line 30: syntax error at or near type word "long"
    "dokimi10.c", line 33: illegal initialization
    "dokimi10.c", line 33: cannot recover from earlier errors: goodbye!
    and the code is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int multiply(long x, long y);
    int divide(long x, long y);
    int add(long x, long y);
    int substract(long x, long y);
    
    long x;
    long y;
    
    int main()
    {
    char temp[900];
    
    printf("Enter a number:\n");
    fgets(temp, sizeof temp, stdin);
    x=atoi(temp);
    
    printf("Enter another number:\n");
    fgets(temp, sizeof temp, stdin);
    y=atoi(temp);
    
    multiply(x,y);
    divide(x,y);
    add(x,y);
    substract(x,y);
    }
    
    
    int multiply(long x, long y);
    {
    int answer;
    answer=x*y;
    printf("i times %i is %i\n", x, y, answer);
    }
    
    int divide(long x, long y);
    {
    int answer;
    
    answer=x-y;
    printf("%i minus %i is %i\n", x, y, answer);
    }

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    this is semi colon;


    int add(long x, long y);

    --------------------^ here you have a semi colon

    functions shouldN'T end with ; unless you are defining them.

    ] <- this is called a square bracket

    (whoops sorry about mis-type there..)
    Last edited by paperbox005; 07-30-2004 at 11:02 PM.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by paperbox005
    functions should end with ; unless you are defining them.

    ] <- this is called a square bracket
    Actually, function prototypes are supposed to end with a semicolon. The code looks fine to me aside from a missing % in the format string for the printf() in the multiply() function.

    It seems your compiler doesn't understand the long type. I bet if you added typedef int long; right after the #include lines it would compile fine, but that is definitely not standard practice. That's an odd one. What compiler are you using?

    It might also be that your compiler doesn't like long by itself. Try int long or long int. And then get yourself a new compiler
    Last edited by itsme86; 07-30-2004 at 09:22 AM.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>function prototypes are supposed to end with a semicolon.
    This is true, though function definitions are supposed to replace the semicolon with a function body, not append a function body after the semicolon.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    doesn work with typedef too!
    I use a grex.org shell accou8nt to learn c! I'm downloading dev c++ 5 nodw

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>doesn work with typedef too!
    The following is your exact code with only two changes made. I removed the semicolons between the parameter list and body of your multiply and divide functions. The code now compiles, but with warnings that you should have no trouble diagnosing.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int multiply(long x, long y);
    int divide(long x, long y);
    int add(long x, long y);
    int substract(long x, long y);
    
    long x;
    long y;
    
    int main()
    {
      char temp[900];
    
      printf("Enter a number:\n");
      fgets(temp, sizeof temp, stdin);
      x=atoi(temp);
    
      printf("Enter another number:\n");
      fgets(temp, sizeof temp, stdin);
      y=atoi(temp);
    
      multiply(x,y);
      divide(x,y);
      add(x,y);
      substract(x,y);
    }
    
    
    int multiply(long x, long y)
    {
      int answer;
      answer=x*y;
      printf("i times %i is %i\n", x, y, answer);
    }
    
    int divide(long x, long y)
    {
      int answer;
    
      answer=x-y;
      printf("%i minus %i is %i\n", x, y, answer);
    }

  9. #9
    meow nbk's Avatar
    Join Date
    Jul 2004
    Posts
    45
    Princeton, where'd the add and subtract funtions go?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int multiply(long x, long y);
    int divide(long x, long y);
    int add(long x, long y);
    int subtract(long x, long y);
    
    long x;
    long y;
    
    int main()
    {
       char temp[900];
    
       printf("Enter a number:\n");
       fgets(temp, sizeof temp, stdin);
       x=atoi(temp);
    
       printf("Enter another number:\n");
       fgets(temp, sizeof temp, stdin);
       y=atoi(temp);
    
       multiply(x,y);
       divide(x,y);
       add(x,y);
       subtract(x,y);
    }
    
    int multiply(long x, long y)
    {
       int answer;
       answer=x*y;
       printf("i times %i is %i\n", x, y, answer);
    }
    
    int divide(long x, long y)
    {
       int answer;
       answer=x/y;
       printf("%i divided by%i is %i\n", x, y, answer);
    }
    
    int add(long x, long y)
    {
       int answer;
       answer=x+y;
       printf("%i plus %i is %i\n", x, y, answer);
    }
    
    int subtract(long x, long y)
    {
       int answer;
       answer=x-y;
       printf("%i minus %i is %i\n", x, y, answer);
    }
    P.S. Princeton - I know the two warnings(no add or sub), but you also misspelled the substract prototype - it's subtract(maybe three warnings? eh) (thanks for pointing that out)
    Last edited by nbk; 07-30-2004 at 01:42 PM.

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>Princeton, where'd the add and subtract funtions go?
    I copied the code from cogeek's second post. The add and subtract functions were not present, nor did I try to run the code as the question was concerning a compile-time error.

    >>but you also misspelled the substract prototype
    I only removed the offending semicolons. All other issues should be taken up with the author of the code, cogeek.
    Code:
    #include <cstdio>
    #include <cstdlib>
    This will fail to compile as C, and should fail to compile as C++ because all standard library names are in the std namespace. You failed to qualify the names you used with std::.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Question about linux compiling with GCC
    By elsheepo in forum C++ Programming
    Replies: 23
    Last Post: 03-07-2008, 11:36 PM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Simple Compiling question ...
    By hypertension in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2003, 01:02 PM