I coded a little program for math actions, but the goto
command won't compile in some cases. Please help me.
Here is the code:
#include<stdio.h>
#define line11
#define YorN
#define End
#define Start
int main(void)
{
float userNumber1,userNumber2;
int mathAction;
char tryAgain;
Start:
printf("Please write 2 numbers for your math action(a,b):\n");
scanf("%f %f",&userNumber1,&userNumber2);
Line11:
printf("Please choose 1 of the following math actions by writing the action number :\n"
"1)Adding a+b\n"
"2)Subtracting a-b.\n"
"3)Multiplying a*b.\n"
"4)Dividing a/b.\n");
scanf("%d",&mathAction);
if (mathAction>4 || mathAction<1) //If the user pick something that's not 1-4 , then this if is going on.
{
printf("Grrrr.. pick between 1-4 ! \n");
goto Line11;
}
if (mathAction==1)
{
printf("Adding %d + %d is %d\n",userNumber1,userNumber2,userNumber1+userNumber2);
}
if (mathAction==2)
{
printf("Subtracting %d - %d is %d\n",userNumber1,userNumber2,userNumber1-userNumber2);
}
if (mathAction==3)
{
printf("Multiplying %d * %d is %d\n",userNumber1,userNumber2,userNumber1*userNumber2);
}
if (mathAction==4)
{
if (userNumber2==0)
{
printf("You cant divide %d by 0 , its math error!\n");
printf("Do you want to choose again? y/n\n");
YorN:
scanf("%c",&tryAgain)
if (tryAgain !='y' || tryAgain !='n')
{
printf("Choose only yes or no by y for yes and n for no.");
goto YorN;
}
if (tryAgain=='y')
{
goto Start;
}
if (tryAgain=='n')
{
goto End;
}
}
printf("Dividing %d / %d is %.2f\n",userNumber1,userNumber2,userNumber1/userNumber2);
}
printf("Thanks for using my program !\nDo you want to try again? (Answer y/n)");
scanf("%d",&tryAgain);
if (tryAgain !='y' || tryAgain !='n')
{
printf("Choose only yes or no by y for yes and n for no.");
goto YorN;
}
if (tryAgain=='y')
{
goto Start;
}
if (tryAgain=='n')
{
goto End;
}
End:
system("PAUSE");
return (0);
}
Here is the compile error:
q5.c: In function 'main':
q5.c:12:11: error: expected expression before ':' token
Start:
^
q5.c:45:12: error: expected expression before ':' token
YorN:
^
q5.c:69:15: error: expected identifier or '*' before ';' token
goto YorN;
^
q5.c:73:16: error: expected identifier or '*' before ';' token
goto Start;
^
q5.c:77:14: error: expected identifier or '*' before ';' token
goto End;
^
q5.c:79:8: error: expected identifier or '*' before ';' token
End:
^
I already tried everything to make this work, please help!
By the way, one of the goto
s is actually working.