(wL) Forums
C: Else misplaced. - Printable Version

+- (wL) Forums (https://war-lords.net/forum)
+-- Forum: Resources (https://war-lords.net/forum/forum-31.html)
+--- Forum: Programming (https://war-lords.net/forum/forum-33.html)
+--- Thread: C: Else misplaced. (/thread-9732.html)



C: Else misplaced. - Danger255 - Jan 27 2013

Code:
#include "stdio.h"
#include "conio.h"
#include "math.h"
void welcome();
#pragma startup welcome
#pragma warn-rch
int main()
{
int a,b,c,menu;
printf("Enter 1,2,3 or 4\n\n");
printf("1.Addition\n");
printf("2.Subraction\n");
printf("3.Division\n");
printf("4.Multiplication\n");
scanf(" %d",&menu);
if(menu==1)
{
printf("Enter any two no.s\n");
scanf(" %d%d",&a,&b);
goto add;
}
else if(menu==2)
{
printf("Enter any two no.s\n");
scanf(" %d%d",&a,&b);
goto sub;
}
else if(menu==3)
{
printf("Enter any two no.s\n");
scanf(" %d%d",&a,&b);
goto div;
}
else if (menu==4)
{
printf("Enter any two no.s\n");
scanf(" %d%d",&a,&b);
goto multi;
}
add:
c=a+b;
printf("Addition of two no. is = %d\n",c);
div:
c=a/b;
printf("Division of two no. is = %d\n",c);
multi:
c=a*b;
printf("Multiplication of two no. is = %d\n",c);
sub:
c=a-b;
printf("Subraction of two no. is = %d\n",c);
getch();
return 0;
}
void welcome()
{
printf("Danger -- Welcomes you!\n\n");
}
The code is not fully done yet still working but this error annoying meh. OR should I use switch at place of ifelse

Double post

Guys the else done now help me getting out I mean


Code:
add:
c=a+b;
printf("Addition of two no. is = %d\n",c);
div:
c=a/b;
printf("Division of two no. is = %d\n",c);
multi:
c=a*b;
printf("Multiplication of two no. is = %d\n",c);
sub:
c=a-b;
printf("Subraction of two no. is = %d\n",c);

So all get printed I want the one I choose .......


GOT THE SOLUTION MYSELF!!


RE: C: Else misplaced. - gOdzOOki - Apr 17 2013

This is probably wayyy old but... dude don't use goto's in C. like, never. worse than raping a nun on her birthday.

Instead, turn all of those into separate subroutines and call them from within your switching statement.

Just curious, but did you come from an assembly background.... or some OOP language?


RE: C: Else misplaced. - Danger255 - Apr 21 2013

I no its old and I studied in a book to not to use gotos
not even tried oop or assembly langs


RE: C: Else misplaced. - gOdzOOki - Apr 22 2013

Yes. Virtually any book on higher level languages will tell you not to use goto's. I asked about assembly because goto's or "branching" is the only way to implement looping constructs. However, if you want to learn C and C derived programming languages... or for any higher level programming language for that matter, you need to learn how to properly use control structures(if-else) and looping constructs (while, for, do-while). Using goto's not only hinders your learning of these language agnostic ideas, it also denies you the ability to learn problem solving techniques that would serve you for a long time to come. Hope this helped!


RE: C: Else misplaced. - Danger255 - Apr 24 2013

yes it helped bro and well Yeah I am learning java now
I will post difficulties Tongue hope u dont mind


RE: C: Else misplaced. - gOdzOOki - Apr 28 2013

np