Thread: Basic :else if program

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    8

    Basic :else if program

    Hi,

    First of all a great site ,Hope beginner's like me can learn from this forum
    I have a problem whilst running the program:Below code runs fine but when I make some changes to it some issues crop up .Please have a look
    Correct Code:
    -------------------------------------------------------------------
    Code:
    /*Power charges calculator
    0 - 200 units each unit 0.75
    201-400 units -100 + 1 for each unit above 200
    401 - 600 units -250 + 1.25 for each unit exceeding 400
    601 and above -450 +1.5 for each unit exceeding 600*/
    
    #include<stdio.h>
    int main()
    {
        int units;
        float charges;
        
        printf("Please enter the number of units consumed rounded to the nearest number:");
        scanf("%d",&units);
        fflush(stdin);
        
        if (units<=200)
        charges = 0.75 * units;
         
        else if (units<=400)
        charges = (400-units)+100;
       
        else if (units<=600)
        charges = (600-units)*1.25 + 250;
        
        else 
        charges = (units-600)*1.5 + 450;
        printf("The charges for the current billing cycle is %.2f",charges);
        
        
        getchar();
    }
    --------------------------------------------------------------------

    The above code works perfectly fine but if I do add a printf at the end of every statement it throws up the following error : syntax error before "else"
    ----------------------------------------------------------------
    Code:
    /*Power charges calculator
    0 - 200 units each unit 0.75
    201-400 units -100 + 1 for each unit above 200
    401 - 600 units -250 + 1.25 for each unit exceeding 400
    601 and above -450 +1.5 for each unit exceeding 600*/
    
    #include<stdio.h>
    int main()
    {
        int units;
        float charges;
        
        printf("Please enter the number of units consumed rounded to the nearest number:");
        scanf("%d",&units);
        fflush(stdin);
        
        if (units<=200)
        charges = 0.75 * units;
        printf("The charges for the current billing cycle is %.2f",charges);
         
        else if (units<=400)
        charges = (400-units)+100;
        printf("The charges for the current billing cycle is %.2f",charges);
    
        else if (units<=600)
        charges = (600-units)*1.25 + 250;
        printf("The charges for the current billing cycle is %.2f",charges);
    
        else 
        charges = (units-600)*1.5 + 450;
        printf("The charges for the current billing cycle is %.2f",charges);
        
        
        getchar();
    }
    --------------------------------------------------------------

    My issue when the code throws up an error before the else clause due to the printf statement .Provided the if statement is true it should run the printf statement (even those in the else if clauses) because as far as my understanding goes, with the curling braces all the else statement controls is the next line rest of the lines should be executed without any dependence on the else if statement.

    My understanding is the code should run but will probably print out the printf under all the conditions with no dependence of the if or else if statements.Please do help me out and let me know why this is not the case.

    Regards
    Last edited by Salem; 07-22-2007 at 02:24 PM. Reason: Use code tags AROUND code, not just added to make it STFU

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    If i understood the problem correctly, you are getting some error when compiling this code because there is no matching if for the else statements.

    Example:

    This is correct
    Code:
    if (...)
    	instruction;
    
    else
    	instruction;
    but this isn't
    Code:
    if (...)
    	instruction1;
    
    instruction2;
    else
    	instruction;
    since the else statement doesn't have any if statement to match. If you have more than one instruction, you have to use the {}.
    Like this:
    Code:
    if (...)
    {
    	instruction1;
    	instruction2;
    }
    
    else
    	instruction;

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    8
    Thanks for the reply but my query is

    if(statement)
    instruction;
    instruction;
    if the statement is in true state it executes the first line and exits the program else it prints the next instruction in that scenario as far as the program I wrote is concerned

    if (statement)
    instruction1; if true print this else it should print the next line

    instruction2;

    else if ( my understanding is there is no such thing as else if in c all it is is a nested if)
    instruction3;

    insruction4,/* if the previous statement was false it shud have printed instruction 2 then check the else if statement if this is false then it shud print statement 4 and go on from there onwards to the next if

    else if ;
    instruction5; if this is else if statement returns true then it should be printing this and exit the program else shud print the next line and check for the true clause .

    I am hoping that my thinking of how the program executes is correct ,if I am correct then the initial statement where I am getting an error should compile.

    Thanks in advance for the replies

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You'll have to add curly braces to your if-else statements :

    Code:
    if(condition)
    {
            statements;
    }
    else
    {
            statements;
    }
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    when including header files such as studio.h it is supposed to look like #include "studio.h" instead of <studio.h>

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    That's more a matter of preference. Typically people use <stdio.h> for headers that they did not create, whereas "myinclude.h" is used for headers they did.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Usually one uses "" for user-defined or library header files and <> for "built-in" header files. This is because you can add to the search path for "", but not (easily) for <>. So, for example, you'll often see
    Code:
    #include "SDL.h"
    even though the programmer in question didn't actually create SDL.h. It's part of a library.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 09:01 AM
  3. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. Help me with this basic c program
    By [ToXiC]-[LeaK] in forum C Programming
    Replies: 1
    Last Post: 01-28-2002, 11:44 PM