# Thread: Help in C with 'if' statements

1. ## Help in C with 'if' statements

I am writing a simple program in C, but I cannot figure out why my if statement is not working. when I compile and run, it ignores the third scanf and whatever the user inputs, it skips the statement and just uses the initial value for the variables. Here is the code that I have. Someone please help me it is very annoying.

Code:
```/* Program to calculate weight gained and money spent from beer consumption */
//Program By: James T Garner - 2006

#include <stdio.h>

int main(void)

{
//declare variables
double b, c, cal=150, year=365, leap;
int yes=1, no=2;

//User Input

printf("\nOn average, How many beers (12 oz) do you drink a day?\n");
scanf("\n%lf", &b);

printf("\nOn average, How much money (US dollars) do you spend on each beer?\n");
scanf("\n%lf", &c);

printf("\nIs it Leap Year? (1 = yes, 2 = no)\n\n");
scanf("%lf"), &leap;

//If statement

if     (leap == 1)
cal = 150.4, year = 366;

//Result Output

printf("\nYou will drink about %.2f beers this year.\n", b*year);
printf("\nYou will spend about \$%.2f this year on beer itself.\n", (b*year)*c);
printf("\n\nUnfortunately, you will gain %.2f calories from beer,\n", (b*year)*cal);
printf("\nWhich is equal to a gain of %.2f pounds from drinking beer.\n\n", (cal*b*year)/3500);

//Disclaimer

printf("\n\n");
printf("\nCalculations are based on information from www.realbeer.com\n\n");

//System pause to pause the program window for viewing
system("pause");

return 0;

}```

2. You have a syntax error on your scanf statement. Look at it closely.

On another note...

Make leap an integer.

No, I didn't say compare it to 1.0, I said, make it an integer. Floating points have a percentage of error and as such should never be used for anything that requires exact precision including checking for equality to 1. In fact, unless your expecting a decimal out of the first question, I'd make that an integer, as well.

... unless you want Quzah to use this, then you should make it a long or perhaps even an __int64.

OH AND USE CODE TAGS!!!!!

3. Originally Posted by jamestgarner
[...] when I compile and run, it ignores the third scanf and whatever the user inputs, it skips the statement and just uses the initial value for the variables.
And maybe just avoid scanf and always read user input as a string and convert if necessary.

4. > scanf("%lf"), &leap;
Watch the position of the )

This means, call scanf to read in a double and store it in some unspecified random memory location. Then take the address of leap and then throw that address away (a no-operation in effect).

> if (leap == 1)
Use an int if you want a flag type variable. Later on you will learn that using == and != to compare floats and doubles is a bad idea.

> cal = 150.4, year = 366;
Use {} to group statements, not commas.

> You have a syntax error on your scanf statement. Look at it closely.
Except it isn't, it's just unusual use of the comma operator

5. Originally Posted by Salem
Except it isn't, it's just unusual use of the comma operator
I understood that it was legal, I was just pointing in the direction of the error. Perhaps, "syntax error" was a bad choice of words. More like a syntax anomaly.

6. ## much too fat and a little too long

Originally Posted by jamestgarner
Code:
```   double b, c, cal=150, year=365, leap;

if     (leap == 1)
cal = 150.4, year = 366;

printf("\n\nUnfortunately, you will gain %.2f calories from beer,\n", (b*year)*cal);```
Why do you change the calorific content of beer in a leap year?

b is beers per day, year is days per year, cal is calories per beer, days per year is incremented in leap years

I can't see what chain of thought leads you to increase calories per beer in a leap year or why a 0.27% increase was deemed suitable. I'm sure too much beer comes into the analysis somewhere but is that on my part or yours.