Gcc doesn't seem to get my scanf right, but Visual Studio does, what's wrong?

This is a discussion on Gcc doesn't seem to get my scanf right, but Visual Studio does, what's wrong? within the C++ Programming forums, part of the General Programming Boards category; First I actually thought there was something wrong with my programming skills. But after I compiled the same code using ...

  1. #1
    Registered User Bangonkali's Avatar
    Join Date
    Dec 2010
    Posts
    9

    Gcc doesn't seem to get my scanf right, but Visual Studio does, what's wrong?

    First I actually thought there was something wrong with my programming skills. But after I compiled the same code using Visual Studio, it worked. So here's the code that doesn't seem to be working right with gcc compilers, but they do compile. Note I'm using the Cygwin 3.4.4 version of gcc and 4.5.1 version of gcc from MinGW.

    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
        long double r = 0;
        long double p = 0;
    
        printf ("Enter a number: ");
        scanf ("%Lf", &r);
        printf ("Enter its power: ");
        scanf ("%Lf", &p);
        printf ("%Lf to the power of %Lf = %Lf\n", r, p, pow(r, p));
    
        printf ("7 ^ 3 = %Lf\n", pow (7.0,3.0));
        printf ("4.73 ^ 12 = %Lf\n", pow (4.73,12.0));
        printf ("32.01 ^ 1.54 = %Lf\n", pow (32.01,1.54));
        return 0;
    }
    This is the first one. It compiles with GCC but the output doesn't seem to be what I'm expecting. (MinGW gcc (GCC) 4.5.1)
    Code:
    Enter a number: 5
    Enter its power: 3
    5.000000 to the power of 0.000000 = 0.000000
    7 ^ 3 = 343.000000
    4.73 ^ 12 = 125410439.217423
    32.01 ^ 1.54 = 208.036691
    The part "5.000000 to the power of 0.000000 = 0.000000" doesn't seem to be right. But on the other hand, when I compiled it using Visual Studio, it worked very well.
    Code:
    Enter a number: 5
    Enter its power: 3
    5.000000 to the power of 3.000000 = 125.000000
    7 ^ 3 = 343.000000
    4.73 ^ 12 = 125410439.217423
    32.01 ^ 1.54 = 208.036691
    I looked for a solution in the internet and I found sscanf. I tried to modified the code, it still compiles with gcc but yielding the same results.
    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
        char line[100];
        long double r = 0;
        long double p = 0;
       
        printf ("Enter a number: ");
        fgets(line,sizeof(line),stdin);
        sscanf(line,"%Lf",&r);
        
        printf ("Enter its power: ");
        fgets(line,sizeof(line),stdin);
        sscanf(line,"%Lf",&p);
        
        printf ("%Lf to the power of %Lf = %Lf\n", r, p, pow(r, p));
    
        printf ("7 ^ 3 = %Lf\n", pow (7.0,3.0));
        printf ("4.73 ^ 12 = %Lf\n", pow (4.73,12.0));
        printf ("32.01 ^ 1.54 = %Lf\n", pow (32.01,1.54));
        return 0;
    }
    Using the gcc compiler of MinGW this is the result: (MinGW gcc (GCC) 4.5.1)
    Code:
    Enter a number: 5
    Enter its power: 3
    5.000000 to the power of 0.000000 = 0.000000
    7 ^ 3 = 343.000000
    4.73 ^ 12 = 125410439.217423
    32.01 ^ 1.54 = 208.036691
    The previous yields the same result as before where the part "5.000000 to the power of 0.000000 = 0.000000" is not working as intended. If I use the compiler from Visual Studio 2008 C++, this is the result:
    Code:
    Enter a number: 5
    Enter its power: 3
    5.000000 to the power of 3.000000 = 125.000000
    7 ^ 3 = 343.000000
    4.73 ^ 12 = 125410439.217423
    32.01 ^ 1.54 = 208.036691
    Last edited by Bangonkali; 12-12-2010 at 12:10 PM.

  2. #2
    Registered User Bangonkali's Avatar
    Join Date
    Dec 2010
    Posts
    9
    I just found out why this is happening but I still have a problem. According to someone who replied to my email it was because gcc was keeping a space in the keyboard buffer. And you can tell if the same thing's happening again if the second input is always 0. He said I should try to use the following code separated by a space instead:

    Code:
    scanf( "%lf %lf", &r, &p );
    I edited my original code to work like this:

    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
        // my personal troubles with long double
        // while using gcc under cygwin and mingw
        long double r = 0;
        long double p = 0;
        long double a = 0;
        
        printf ("Enter a number <space> power: ");
        scanf( "%Lf %Lf", &r, &p );
    	
        // r to the power of p
        a = pow(r,p);
    
        printf ("%.2Lf to the power of %.2Lf = %.2Lf\n", r, p, a);
        printf ("7 ^ 3 = %.2Lf\n", (long double)pow(7.0,3.0));
        printf ("4.73 ^ 12 = %.2Lf\n", (long double)pow(4.73,12.0));
        printf ("32.01 ^ 1.54 = %.2Lf\n", (long double)pow(32.01,1.54));
        return 0;
    }
    Now it works perfectly fine with the following output when compiled with gcc both from MinGW and Cygwin.

    Code:
    5.00 to the power of 3.00 = 125.00
    7 ^ 3 = 343.00
    4.73 ^ 12 = 125410439.22
    32.01 ^ 1.54 = 208.04
    I still don't know how accept value from a second scanf though. If anyone of you who knows how to circumvent this issue please help. I need to accept the second input only after the return has been pressed from the first input and not separated by space.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    We did all this yesterday.
    C and Cpp GCC Compiler Issues
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Why bother, eh Salem?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    And done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 03:58 PM
  2. Undefined reference to.. probably Makefile problem
    By mravenca in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 04:29 AM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. OpenGL prob
    By HQSneaker in forum Game Programming
    Replies: 18
    Last Post: 06-22-2006, 12:55 PM
  5. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21