Thread: My first and simple program

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    32

    My first and simple program

    Hi there

    I'm still new to this Programming thing so bear with me:

    I'm trying to set a program that reads like this:

    -How many cakes did you eat today?
    __


    -You gained __ lbs.


    here are my variables:


    5000 Calories = 1 cake
    1 lbs = 3500 Calories


    heres my setup:


    #include<stdio.h>
    #define CALORIES_IN_CAKE 5000
    #define CALORIES_IN_POUND 3500

    int main (void) {

    double pounds_in_cake, num_cakes;

    pounds_in_cake=(CALORIES_IN_CAKE)/CALORIES_IN_POUND;

    printf("How many cakes did you eat today?\n");
    scanf("%lf", &num_cakes);
    printf("You gained %.2lf pounds.\n",

    pounds_in_cake*num_cakes);

    system("PAUSE");
    return 0;

    }


    However, my output is wrong.

    I cant seem to find the problem

    Any response will greatly appreciated

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The problem is this line:
    Code:
    pounds_in_cake=(CALORIES_IN_CAKE)/CALORIES_IN_POUND;
    This does integer arithmetic, so pounds_in_cake will always equal 1. To fix this, cast one of the values to a double to ensure that floating point arithmetic is done:
    Code:
    pounds_in_cake=(double)CALORIES_IN_CAKE/CALORIES_IN_POUND;
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    Thanks birthtub!

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    32

    Got another one.

    Here's another program I want to setup:

    -How many people did you kill today?
    __


    You will serve __ of years in jail


    so my variables are:

    For every one person you kill, you will serve 7 years




    #include <stdio.h>
    #define YEARS_EACH_PEOPLE=7
    #define PEOPLE_KILLED=1

    int main (void) {

    int years_served_per_killing, num_killings;

    years_served_per_killing=YEARS_EACH_PEOPLE*PEOPLE_ KILLED;

    printf("How many people did you kill today?\n");
    scanf("%d", num_killings);
    printf("You will serve %d of years in jail.\n", years_served_per_killing*num_killings);

    system("PAUSE");
    return 0;

    }


    I get an error in :

    ears_served_per_killing=YEARS_EACH_PEOPLE*PEOPLE_K ILLED

    dont really understand what the problem is though

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Neotriz View Post
    Thanks birthtub!
    bithub has a new nickname!!!!

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Youve got '=' char in your marco. take out that and also you've got a space in between People_ Killed.

    Code:
    #define YEARS_EACH_PEOPLE=7
    #define PEOPLE_KILLED=1
    Code:
    years_served_per_killing=YEARS_EACH_PEOPLE*PEOPLE_ KILLED;
    To be honest, you dont need to find the year)_served_per_killing. You could just getting the no of people killed * YEARS_EACH_PEOPLE

    Thats should do it.

    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Kennedy View Post
    bithub has a new nickname!!!!
    Thats wicked man!!!! lol

    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    ssharish, I still get the error.

    what do you mean by " '=' char in my marco"?

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by ssharish2005 View Post
    Thats wicked man!!!! lol

    ~ssharish
    Yes, that's a winner.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Kennedy View Post
    bithub has a new nickname!!!!
    At least he didn't call me bathtub. Although now that I think about it, birthtub may be worse...

    what do you mean by " '=' char in my marco"?
    You need to get rid of the equals sign.
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    Quote Originally Posted by bithub View Post
    At least he didn't call me bathtub. Although now that I think about it, birthtub may be worse...

    You need to get rid of the equals sign.
    sorry for the nickname. It is rather humorous haha

    But anyway, i cant seem to get it work at all

    Can you post the whole script and see if it works?

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    scanf() should take a pointer to an integer.
    Code:
    scanf("%d", &num_killings);
    bit∙hub [bit-huhb] n. A source and destination for information.

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    32
    Still not working


  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What does the compiler or output look like?

  15. #15
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You should learn to use code tags when you post your programs.
    Last edited by kermit; 09-01-2009 at 06:39 PM.

Popular pages Recent additions subscribe to a feed