Thread: Trying to do basic math functions. Why doesn't this work?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    43

    Trying to do basic math functions. Why doesn't this work?

    Hello,
    I'm trying to write a simple program that calculates projectile distance and time. I was given the basic equations but I'm writing something incorrectly because my output is currently zero. I have a feeling this may be an explicit type casting issue but I'm not sure what to do:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main(void)
    {
       const double PI = 3.14, GRAVITY = 9.8;
       double degrees = 0, velocity = 0, angle = 0, range = 0, time = 0 ;
    	
    	
       printf("Enter the measure of the angle in degrees. \n");  
       scanf("%d", &angle);
    	
       printf("Enter the initial velocity. \n");  
       scanf("%d", &velocity);
    	
       angle = angle * (PI/180.0);
       range = (sin(2 * angle)) * pow(velocity, 2)/ GRAVITY;
       time  = range/(velocity * (cos(angle)));
      	
       printf("The projectile will travel %d meters.\n", range);
       printf("It will take %d seconds.\n", time);
       
       return 0;
    }
    Can anyone offer hints as to how I should fix this?

    Thanks in advance,
    crazychile

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not use &#37;d to read a double.
    I believe the correct is %lf (but check the docs to be sure).
    And don't print doubles using %d; use %f.

    It's YOUR responsibility to tell scanf/printf/etc the correct type you are passing to it (this can be avoided in C++, but not C). If you do not, you will get unexpected results or crashes. Or simply put, undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    To clarify, the program compiles but the output values for the print statements are each zero.
    Thank you,
    crazychile

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, I'm sure it does, but this is a runtime error, not a compile error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    ALL HAIL ELYSIA!!!!


    That worked!
    Thanks,
    crazychile

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depending on which compiler you are using, enabling warnings may actually tell you that the format for the printf and scanf are wrong. It would be a warning, but it's still a lot more help than trying to figure out yourself what you got wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions won't work
    By Will_rookwood in forum C++ Programming
    Replies: 5
    Last Post: 04-13-2006, 09:02 AM
  2. handling basic math functions
    By MyDestiny in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2005, 01:12 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM