Tuesday, July 14, 2009

What is wrong with my program? [help in c programming]?

#include %26lt;stdio.h%26gt;


int main(void)





{


float x,y, result;


char cop1;





printf("Choose among the following operators:\n+,-,*,/", cop1);


scanf("%c", %26amp;cop1);





if (cop1=='/')


{


printf("Enter Dividend\n", x);


scanf("%f",%26amp;x);


printf("Enter Divisor\n", y);


scanf("%f", %26amp;y);


printf("result=%f\n",x/y);


}


else if (cop1=='+')


{


printf("Enter 1st addend\n", x);


scanf("%f",%26amp;x);


printf("Enter 2nd addend\n", y);


scanf("%f", %26amp;y);


printf("sum=%f\n",x+y);


}


{


else if (cop1=='-');


printf("enter minuend\n", x);


scanf("%f",%26amp;x);


printf("enter subtrahend",y);


scanf("%f",%26amp;y);


printf("difference=%f", x-y);


}


{


else if (cop=='*');


printf("enter 1st factor\n", x);


scanf("%f",%26amp;x);


printf("enter 2nd factor\n",y);


scanf("%f",%26amp;y);


printf("product is=%f\n",x*y);


}


else


printf("Wrong!\n");





return=0;


}





calculator.c: In function 'main':


calculator.c:28: error: parse error before 'else'


calculator.c:36: error: parse error before 'else'


calculator.c:43: error: parse error before 'else'

What is wrong with my program? [help in c programming]?
Here are the reasons why it won't currently compile (some of which you haven't found yet):





[[ 1 ]] You have opening braces in the wrong places:


{


else if (cop=='*');


They should be right after the else-if clause, not before.





[[ 2 ]] You have a semicolon after some else-ifs:


else if (cop=='*');


Take them out.





[[ 3 ]] The last else-if says "if (cop=='*')." It should be cop1, not cop.





[[ 4 ]] Finally, at the very end, you have "return=0;" Leave out the "=". It's just "return 0;"





Fix those errors and your program seems to work correctly. Nice job.
Reply:First of all, you have an opening brackets "{" right before else as well as inconsistent if{}else{} statements.


I'd suggest you to utilize indentation so you would see where your clauses begin and end.
Reply:Hi!





Seeing your various printf statements doesn't allow me to believe that you could make you a small mistake in the beginning. You could program very well.





Errors:


1. Your first printf statement. The quote doesn't include the variable name or instead remove if not needed.





2. Misplaced curly brackets. You have done well in your first two if..else blocks.





3. No semi-colon after if condition.





4. return 0;





Advise:


You can better improve your program if you would have used printf statements for once in the beginning i.e. before the if..else blocks instead of placing them in each block.





Sorry but you have to look for own small mistakes.


Enjoying programming at your level best. Try harder and harder, if unsuccessful then make a request. Goodluck!





Gagan..


No comments:

Post a Comment