Thread: Help in C with 'if' statements

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    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;  
        
    }
    Last edited by Dave_Sinkula; 09-06-2006 at 09:25 PM. Reason: Added [code][/code] tags -- learn to use them yourself.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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!!!!!
    Last edited by SlyMaelstrom; 09-06-2006 at 09:26 PM.
    Sent from my iPadŽ

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote 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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote 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.
    Sent from my iPadŽ

  6. #6
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72

    much too fat and a little too long

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. C++ If Statements Help
    By moporho in forum C++ Programming
    Replies: 19
    Last Post: 01-18-2008, 08:40 AM
  4. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM