-
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
-
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;
-
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:)
-
You'll have to add curly braces to your if-else statements :
Code:
if(condition)
{
statements;
}
else
{
statements;
}
-
when including header files such as studio.h it is supposed to look like #include "studio.h" instead of <studio.h>
-
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.
-
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
even though the programmer in question didn't actually create SDL.h. It's part of a library.