-2

I've been working on a console based calculator app, and I wanted to use 2 functions in order to make it look cleaner (I didn't want main having too many lines), so I decided to use goto to jump from main to my foil function, then another goto to jump back to the start of main. I was just wondering if it's unsafe to do this. Thanks :)

void foileq()
{
    int a, b, c, d;
    printf("Enter the 4 numbers\n");
    cin >> a;
    cin >> b;
    cin >> c;
    cin >> d;
    cout << a * c << " " << a * d << " " << b * c << " " << b * d << endl;
}

int main()
{
    float a, b;
    string type = "";
BEGIN:
    {
        while (1)
        {
            printf("Add,subtract,multiply,divide,foil,power?\n");
            cin >> type;
            if (type == "foil")
            {
                goto FOIL;
                continue;

            }
            else
            {
                printf("Enter A number\n");
                cin >> a;
                printf("Enter another number\n");
                cin >> b;
                if (strcmp(type.c_str(), "add") == 0)
                    printf("%.2f\n", a + b);
                else if (strcmp(type.c_str(), "subtract") == 0)
                    printf("%.2f\n", a - b);
                else if (strcmp(type.c_str(), "multiply") == 0)
                    printf("%.2f\n", a * b);
                else if (strcmp(type.c_str(), "divide") == 0)
                    printf("%.2f\n", a / b);
                else if (strcmp(type.c_str(), "power") == 0)
                    printf("%.2f\n", pow(a, b));
            }
        }
    }
FOIL:
    foileq();
    goto BEGIN;
}
Jeff
  • 56
  • 8
  • In this case I (no goto hater) think it is bad, the main is an infinite loop –  Jun 04 '15 at 18:08
  • 7
    `float main()`??? – NathanOliver Jun 04 '15 at 18:11
  • Please provide a proper example - see @NathanOliver comment (bad enough for a down-vote) –  Jun 04 '15 at 18:12
  • yeah I'm not really sure why I changed it from its usual to float, i guess b/c I was using floats inside the function, but yeah it was unnecessary to do that. I fail to see how the example isn't proper, because it compiles and runs fine, even though it's typically going to be int/void. – Jeff Jun 04 '15 at 18:13
  • 3
    Don't jump backwards using `goto`! That's one of the rules, those crappy MISRA standards got right. – πάντα ῥεῖ Jun 04 '15 at 18:15
  • Fixed that float/void main thing –  Jun 04 '15 at 18:26
  • I was wondering, why would it be improper to use void instead? – Jeff Jun 04 '15 at 18:27
  • @Jeff: that question has already been asked many times, for example at http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c. I encourage you to do some research when you have this kind of questions; 99% of the times they have already been asked (and answered!). – Fabio says Reinstate Monica Jun 04 '15 at 19:48

3 Answers3

3

If you call foileq(); instead of goto FOIL; the behaviour would be the same. In this case using goto does not make things more readable. There are very rare occasions where goto makes code better and this is not one of them.

Also the continue you have currently written is not needed because of the goto right before it.

onqtam
  • 4,356
  • 2
  • 28
  • 50
  • I had thought about doing that, but if I did that it would first call this would it not? printf("Enter A number\n"); cin >> a; printf("Enter another number\n"); cin >> b; and ahh I see, idk why I added continue. I remember having a problem with the program closing after executing foileq(), but I guess my 2nd goto fixed that. – Jeff Jun 04 '15 at 18:10
  • @Jeff No it wouldn't. ```continue``` would send you right back to the begining of the ```while``` loop. Also the ```goto FOIL;``` and the ```printf("Enter A number\n"); ``` stuff are in separate then/else scopes of the if statement. – onqtam Jun 04 '15 at 18:12
  • I see what you're saying now, Yeah I could just do if type == "foil", then have foileq called there, didn't think about that. – Jeff Jun 04 '15 at 18:16
0

The seemingly universal revolution to GOTO is largely due to Edsger Dijkstra's letter "Go To Statement Considered Harmful".

(source: while(1) .. break instead of goto )

Use your while loop to exit when type == "foil"

while( type != "foil" )

then change the else to a if( type != "foil" ) to prevent it from running when the input is foil.

Community
  • 1
  • 1
avk
  • 871
  • 1
  • 9
  • 22
  • I'll definitely take a gander at the letter, thanks. Also, is it typical for a question like this to receive 4 downvotes? I thought it was an appropriate question given I'm inexperienced and never used goto before. o.o – Jeff Jun 04 '15 at 18:20
  • @Jeff - Well it is custom to first search the Internet ( or Stack Overflow ) for questions already answered. This can sometimes be difficult if you do not know what exactly to look for. At least you got (some) answers that can help you further. – avk Jun 04 '15 at 18:24
  • True I did get helpful answers, Next time I'll do a bit of research before asking the question, sorry about that. – Jeff Jun 04 '15 at 18:29
  • @Jeff - No apologies necessary. It happens most of us at least once. – avk Jun 04 '15 at 18:30
0

"Is it bad to use goto in this situation?"

It's almost always considered bad to use goto in any situation. If you use it, don't jump backwards but forward only.

Something like the following (using a single label) might be OK:

 int foo() {
     while(loop_condition_ok) {
         if(inner_operation_fails()) {
             goto hell;
         }
     }
     return 0;

 hell:
     return -1;
 }
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Ahh I see, is there a specific reason going backwards with goto would be considered harmful? – Jeff Jun 04 '15 at 18:28
  • _"is there a specific reason going backwards with `goto ...` "_ there are loads of, primarily `goto` makes your code cluttered and unreadable. In general, you just don't use `goto`, but appropriate loop constructs and `break` or `continue` to skip code. – πάντα ῥεῖ Jun 04 '15 at 18:34
  • Of course `do { /** break on failure**/ if(1condition) { break; } } whilefalse);` is better. Where's the _"Gotcha"_ actualy?? – πάντα ῥεῖ Jun 04 '15 at 18:55