Thursday, July 30, 2009

Wonder whats wrong with this break statement?

//in c programming


main()


{


int a,b,c;int flag=0;


printf("enter the sides of triangle");


scanf("%d%d%d",%26amp;a,%26amp;b,%26amp;c);


while(flag==0)


{


if(a==b%26amp;b==c)


{


printf("triangle is equilateral");


break;


}


if(a==b||b==c||c==a)


{


printf(triangle is isoceles");


break;


}


if(a!=b%26amp;%26amp;b!=c%26amp;%26amp;c!=a)


{


printf("triangle is scalene");


break;


}


}//while closes


getch();


}


but compiler shows error in break says misplaced break

Wonder whats wrong with this break statement?
You're missing a quote on "triangle is isoceles."





Other than that, it compiles for me.
Reply:The most common error when there is a misplaced break is not a misplaced break.. Look at your parenthesis and make sure you have enough. Usually you are missing one parenthesis at the end and it will screw all of the code. Also I am not sure if you can use multiple breaks like that. Especially when you dont specifiy where to break to. Sometimes a break can even end all the code and break the program so maybe you could use something else instead of a break. Such as a call function.
Reply:the break statement breaks the all loops so don,t use more than one break statements in the loop
Reply:I compiled this with Visual Studio and found two errors:





You are missing a quote (") on the line that says





printf(triangle is isoceles")





and I noticed one logical error on this line:





if(a==b%26amp;b==c)





it should read:





if ( a==b %26amp;%26amp; b==c )





the single %26amp; means bitwise and but you want a logical and which is two %26amp; (%26amp;%26amp;).





However, I did not get an error saying misplaced break. When I corrected those two errors, the program compiled and ran perfectly.
Reply:main()


{


int a,b,c;int flag=0;


printf("enter the sides of triangle");


scanf("%d%d%d",%26amp;a,%26amp;b,%26amp;c);


while(flag==0)


{


if(a==b%26amp;b==c)


{


printf("triangle is equilateral");


flag=1;


break;


}


if(a==b||b==c||c==a)


{


printf(triangle is isoceles");


flag=1;


break;


}


if(a!=b%26amp;%26amp;b!=c%26amp;%26amp;c!=a)


{


printf("triangle is scalene");


flag=1;


break;


}


}//while closes


getch();


}


No comments:

Post a Comment