-6

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 gotos is actually working.

Joe Kennedy
  • 9,365
  • 7
  • 41
  • 55
Tom Bazak
  • 11
  • 3
  • 1
    Show the code that demonstrates the problem in your post. – Vlad from Moscow Nov 21 '15 at 14:56
  • the code is too long so its not letting me put the whole code , please download and check it. – Tom Bazak Nov 21 '15 at 14:57
  • 3
    Try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) which demonstrates your problem, and show that. And don't post images of text, especially *links* to images. A link may go stale and make your question worthless. If you have text, copy and paste it as text. – Some programmer dude Nov 21 '15 at 15:00
  • E.g `Start:` replace to `:` by `#define Start` – BLUEPIXY Nov 21 '15 at 15:00
  • 1
    Oh and by the way there are very few things you need to use `goto` for. Try and avoid it at all cost. – Some programmer dude Nov 21 '15 at 15:02
  • @JoachimPileborg: Hmm, "try and avoid it at **all** cost" ... as you can always get along without `goto`, there cannot be "few things you need to use goto for" (which is close to almost never in practise;-) I'd say "unless avoiding `goto` will make your code significantly less readable". There is just no rule of thumb when to use `goto`. – too honest for this site Nov 21 '15 at 15:07
  • This code is so 1970ies style. Spaghetti should be on a plate with a good sauce, not on the monitor. Did you convert some old BASIC code? I'd recomment to restructure without `goto` first, C has loop constructs with `break` and `continue`, `else`, etc. – too honest for this site Nov 21 '15 at 16:46

3 Answers3

3

The problem is that you have

#define YorN
#define End
#define Start

in the beginning of your source. These preprocessor macros will be evaluated before compiling into nothing, so this code:

YorN:
goto YorN;

will become

:
goto;

Just remove them, since it make no sense to have them.

Jack
  • 131,802
  • 30
  • 241
  • 343
0

Don't #define your goto labels. Your code was also missing a semicolon ( after scanf("%c",&tryAgain) at label YorN).

Here you can see the corrected version.

For the sake of completeness, I'll post the corrected code here:

#include<stdio.h>

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);
}
collapsar
  • 17,010
  • 4
  • 35
  • 61
0

It won't compile because you have #defined the target symbols out of existence.

Other issues: In most of your printf() format strings you use %d without care for the actual type of your argument. Either cast your objects to int or use the correct type (%f for double).

One final issue: for what you are doing, you should be using functions and loops. Every one of your gotos can be replaced with an appropriate loop.

Hope this helps.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39