# Thread: My first and simple program

1. ## 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. 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;`

3. Thanks birthtub!

4. ## 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. Originally Posted by Neotriz
Thanks birthtub!
bithub has a new nickname!!!!

6. 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

7. Originally Posted by Kennedy
bithub has a new nickname!!!!
Thats wicked man!!!! lol

~ssharish

8. ssharish, I still get the error.

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

9. Originally Posted by ssharish2005
Thats wicked man!!!! lol

~ssharish
Yes, that's a winner.

10. Originally Posted by Kennedy
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.

11. Originally Posted by bithub
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. scanf() should take a pointer to an integer.
Code:
`scanf("%d", &num_killings);`

13. Still not working

14. What does the compiler or output look like?

15. You should learn to use code tags when you post your programs.