Question:
C++ switch case in multiple source file?
2008-09-17 19:33:12 UTC
Little help here, i kinda confuse how to split my switch case program into multiple source file. Can anyone show me an example?

THANKS!!
Three answers:
Jeyhza
2008-09-17 19:56:46 UTC
Well... I don't know if I follow you but you can have the switch statement



myfunctions.h



...

void functionA();

void functionB();





myfunctions2.h

..

void functionC()



file.cpp



#include "myfunctions.h"

#include "myfunctions2.h"



int function()

{

switch( n )

{

case A:

functionA();

break;

case B:

functionB();

break;

case C:

functionC();

break;

default:

return 0;

}

}



and just call the functions from the header file. And you can return from a case statement in a function if you want it will just stop executing and return the value at the time you call it which it always does. If you're unsure of how to split the files up into multiple header/source files there's an excellent article in gamedev.net

http://www.gamedev.net/reference/programming/features/orgfiles/
Ahmed The Ninja
2008-09-17 19:53:30 UTC
Splitting a switch case between files would mean splitting a function between files which is not possible and just does not make any sense.



You can use return instead of break however it has a completely different meaning. Break exits the switch case and continues to with the function, whereas return exits the function, returning the value that you specify to the caller.
2008-09-17 19:42:37 UTC
What compiler are you using?



And, you can use return in a switch statement. I don't know what you mean by "previous screen". It will exit the function.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...