1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C: Visual Studio 2008

  1. Jan 10, 2010 #1
    Hi,

    My visual studioes is acting very weird the following code:

    #include <math.h>
    #define GRAVITY 9.81

    int main(void)
    {
    double range;
    double angle;
    double initspeed;

    printf("enter speed\n");
    scanf("%lf", &initspeed);

    printf("enter angle\n");
    scanf("%lf", &angle);

    range=(initspeed*initspeed)/GRAVITY*sin(2*angle);
    printf("The range is: %f m/s^2\n", range);

    system( "PAUSE" );
    return 0;
    }

    gives an error: 1>formatspecifier - 1 error(s), 2 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    I'm not sure why? My code seems fine does anyone know what the problem may be? I think it's a compiler error.
     
  2. jcsd
  3. Jan 10, 2010 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    scroll up the error messages and it will tell you which line it doesn't like
     
  4. Jan 12, 2010 #3

    MATLABdude

    User Avatar
    Science Advisor

    This might be dumb, but can you use C-style input/output in C++ instead of cin/cout? (Presumably, you're using Visual C++ or Visual C++ Express)

    Ah, I think that might be the problem... You probably need to include the cstdio library to access those sorts of instructions (see the C Library section of the link below):
    http://www.cplusplus.com/reference/

    But yeah, as mgb_phys says, the compiler should tell you which lines your errors are in.
     
  5. Jan 12, 2010 #4

    Borek

    User Avatar

    Staff: Mentor

    Any reason why not to?
     
  6. Jan 14, 2010 #5

    MATLABdude

    User Avatar
    Science Advisor

    Not as long as the right header (stdio.h or cstdio.h) is included! I suppose that you'd also have to include iostream.h to make use of cin/cout.
     
  7. Jan 14, 2010 #6

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    There's nothing wrong with the program.
    It's complaining because you don't have "stdio.h" included so it doesn't know about printf/scanf
     
  8. Jan 14, 2010 #7

    Borek

    User Avatar

    Staff: Mentor

    I expect unexpected output.
     
  9. Jan 14, 2010 #8

    Mark44

    Staff: Mentor

    I concur with mgb_phys that the code needs to #include <stdio.h> and I believe Borek is correct that it (might) produce some unexpected output.

    This %f format specifier in this line should probably be changed to %lf.
    Code (Text):
    printf("The range is: %f m/s^2\n", range);
     
    I would advise reading up on the proper use of the %f and %lf format specifiers used in both scanf() and printf(). They are a little tricky and somewhat nonintuitive.
     
  10. Jan 14, 2010 #9

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Using %f in printf is perfectly valid, the printf call doesn't need to know how big the float is it can handle that when generating the output.

    But in scanf you do need the "lf" so the call knows how big the destination variable is to copy the result into, you can't necessarily copy a float into a double or float at the same address correctly.
     
  11. Jan 14, 2010 #10

    Borek

    User Avatar

    Staff: Mentor

    Surprise, I was wrong for 25 years :bugeye:
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook