Thread: Compiling error using GCC in Unix

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Compiling error using GCC in Unix

    Here is my code and it will compile and run perfectly in Borland, but using gcc in Unix, I get an error, "invalid operand to binary" or something along those lines and I have no idea what to do to correct it. Here is my code:
    Code:
    #include <stdio.h>
    
    int main()
    {
       int num1;
       int num2;
       int den1;
       int den2;
       int finalnum;
       int finalden;
    
       printf("Please enter in the numerator of the first fraction: ");
       scanf("%d", &num1);
       printf("Please enter in the denominator of the first fraction: ");
       scanf("%d", &den1);
       printf("Please enter in the numerator of the second fraction: ");
       scanf("%d", &num2);
       printf("Please enter in the denominator of the second fraction: ");
       scanf("%d", &den2);
       finalnum = (num1*den2)+(num2*den1);
       finalden = den1*den2;
       printf("The answer is %d/%d", finalnum, finalden);
       fflush(stdin);
       getchar();
       return 0;
    }
    [code tags added by ygfperson]

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    post your error? compiles and runs fine for me under gcc 2.7.x.x
    hello, internet!

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    error

    lab1.c: In function "main":
    lab1.c:21: invalids operands to binary &

  4. #4
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    The only problem I see is that you don't need to flush the input stream if your program is just going to end in the next line or so without reading more input. And I think I heard somewhere that fflush(stdin) is bad. Other than that I don't see anything wrong and I can't reproduce the problem on my computer.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  5. #5
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Actually, I can imagine that schools teach bad habits. Judging from all of the terrible source code i see all over the place.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    Well I am going to be a good programmer, but I need to get through the basics first then get some experience. So does anyone have any idea why I am getting the error message when trying to compile with gcc in unix?

  7. #7
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by csmatheng
    Well I am going to be a good programmer, but I need to get through the basics first then get some experience. So does anyone have any idea why I am getting the error message when trying to compile with gcc in unix?
    ensure that the code you have on your screen is EXACTLY what's shown here.
    hello, internet!

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    It is EXACTLY the same except I just removed fflush(stdin) and I still get the same error.

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    I am sorry. It was NOT EXACTLY the same. I found the mistake. I was missing the comma in one of the scanf statements. I have corrected it and it works fine. Now, I need to figure out how to reduce the fraction by finding the greatest common factor.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>And I think I heard somewhere that fflush(stdin) is bad.
    That'd be here.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC error in compiling
    By JFonseka in forum C Programming
    Replies: 11
    Last Post: 10-15-2007, 04:57 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. problems compiling in gcc
    By ra_mer in forum C Programming
    Replies: 6
    Last Post: 09-04-2006, 09:13 PM
  4. Compilation error under Unix with GCC
    By marcusg in forum C Programming
    Replies: 2
    Last Post: 06-01-2005, 08:16 AM
  5. Compiling in Unix vs Visual C++
    By stimpyzu in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 06:41 AM