(wL) Forums

Full Version: C: Else misplaced.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!!
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?
I no its old and I studied in a book to not to use gotos
not even tried oop or assembly langs
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!
yes it helped bro and well Yeah I am learning java now
I will post difficulties Tongue hope u dont mind
np