Good day everyone! Why in this code it is impossible to switch from one function to another using the goto method? The compiler writes that the functions bar, fellodd and Elochka are not defined. Even if the program was able to start, it ends without executing the functions. Somewhere even read that it is impossible to do. (According to the idea of ​​the functions, certain melodies should have been played (Beep). Dots-replacing parts of the code that are not related to the problem.

void bar( ); void feelgood( ); void elochka( ); void improv( ); int main() { ... k=getch(); switch (k) { case 1 : goto bar(); case 2 : goto feelgood(); case 3 : goto elochka(); default : return 0; } // switch return 0; }// main void elochka() { ... } void bar( ) { ... } void feelgood( ) { ... } 
  • Well, probably start with the most common answer in this case - do not use goto NEVER! By sabzh - and where is your .h file? It is precisely in it that the functions are defined and for sure that is why the compiler swears - alexoander
  • This is not a complete code. cut them out because they are displayed incorrectly. #Include <stdio.h> were included #include <conio.h> #include <windows.h> - Oscar
  • And why is goto so bad? And is there a replacement for him in this case? - Oscar
  • @ Oscar, does not work, because there is an incorrect use of the goto operator - Grundy
  • And what has been done wrong? Even if you remove the goto, leaving just the function call, it still doesn’t execute - Oscar

2 answers 2

From the standard C language (6.8.1 Labeled statements)

 1 labeled-statement: identifier : statement 

and

3 Label names.

And finally, with respect to the goto (6.8.6.1 The goto statement)

2 A statement to the statement of the enclosing function.

That is, you can use a goto clause within a single function, and transfer control to a clause preceded by an identifier, followed by a colon.

Obviously, this proposal

 goto bar(); 

at least syntactically incorrect because bar() not a label. And most importantly, you cannot jump from one function to another using goto .

It seems that all you need is to simply call the appropriate functions. You can do this as follows.

 switch (k) { case 1 : bar(); break; case 2 : feelgood(); break; case 3 : elochka(); break; default : return 0; } 

or depending on the type and value stored in the variable k

 switch (k) { case '1' : bar(); break; case '2' : feelgood(); break; case '3' : elochka(); break; default : return 0; } 

    I think you wanted to write like this:

     #include <stdlib.h> #include <stdio.h> #include <string.h> #include <conio.h> void bar( ); void feelgood( ); void elochka( ); void improv( ); int main() { int k = getch(); switch (k) { case '1' : bar(); case '2' : feelgood(); case '3' : elochka(); default : return 0; } // switch return 0; }// main void elochka() { puts("elochka"); } void bar( ) { puts("bar"); } void feelgood( ) { puts("feelgood"); } 

    Compiles and even works - when you press the keys with numbers 1, 2 and 3 displays the names of functions.

    I hope you are not surprised that when you press 1, all three functions are called?

    goto only works with tags . But this is a separate issue, and goto really worth using less often ...

    • As for the three functions at once, I noticed this long ago. But why did this happen? I could not give an answer. And Puts - this is a transition instead of goto? - Oscar
    • @ Oscar, an analogue of printf, but without formatting and displays only strings. - iluxa1810
    • I tried your code, the playback is on, but still, when you press any key, it does not stop and plays further down the list. - Oscar
    • @ Oscar after pressing 1 - there will be 3 answers, 2 - 2 answers, and only 3 will be displayed 1. Why? And because it is necessary after each case to set an interrupt break; (as in the example above) - alexoander
    • When you click "some" program simply ends. There are several functions, because you need to use break . And in general, dashing cavalry attack languages ​​do not learn, it is clear from your questions that you have absolutely no foundation. Start with some "C for dummies", honestly, there will be more sense. - Harry