Thread: unreachable code

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    3

    unreachable code

    << split from How can I make this simple assignment more eloquent ? >>
    i have issue with this program when it runs it shows the error of unreachable code plz anyone tell me how to solve this error in c langugae

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main(void)
    {
    clrscr();
    float num1,num2,add,sub,mul,div;
    char op,ans;
    printf("Enter the number and operation which you want to perform:\n");
    scanf("%f %c %f",&num1,&op,&num2);
    	do
    	{
    	  {
    		 if(op=='+')
    		{
    		add=num1+num2;
    		printf("Resut of addition %f\n",add);
    		break;
    		}
    		else if(op=='-')
    		{
    		sub=num1-num2;
    		printf("Result of subtraction %f\n",sub);
    		break;
    		}
    		else if(op=='*')
    		{
    		mul=num1*num2;
    		printf("Result of multiplication %f\n",mul);
    		break;
    		}
    		else
    		{
    		div=num1/num2;
    		printf("Result of division %f\n",div);
    		break;
    		}
    	printf("Do you want to continue:(y/n):");
    	scanf("%c",&ans);
    	}
    	}
    	while(ans!='n');
    getch();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well you don't need break statements inside an if / elseif chain,
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unreachable code in function main()
    By DavidSS in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2010, 07:57 AM
  2. Needing Assistance;Unreachable code in function main()
    By Mehtabyte in forum C++ Programming
    Replies: 5
    Last Post: 11-19-2008, 02:27 PM
  3. Unreachable code?
    By Leojeen in forum C Programming
    Replies: 15
    Last Post: 09-28-2008, 07:11 PM
  4. warning C4702: unreachable code
    By cpjust in forum C++ Programming
    Replies: 8
    Last Post: 05-05-2008, 03:24 PM
  5. Statement unreachable
    By stino31 in forum C Programming
    Replies: 8
    Last Post: 03-11-2008, 03:31 AM