passing a enum to a function...

Here is some code I am testing with to get a better understanding of enums. Please remember I am a beginner so do not go into super complex solutions...

What I am trying to do is make my code "readable". SO I want a function to handle the printing of the menu text. I would like to simple have the function call menu(PlayAgain0; for example.... as this is readable.. .then

Anyway.. I have got this code working.. but it requires me to initialise the enume outside of main as a kind of global variable.

Other variables I have used in functions like string and int and stuff, I could just initialise a variable with the same name as in main... is there a way to do this with enum?

It just seams bad to have the enume variable as a variable getting initialised outside main.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// More Enum Tests
#include <iostream>
using namespace std;

enum EMenu {NewGame, PlayAgain, RestartGame, Instructions} emenu;
//Function Deceleration
void funcMENU (EMenu eMenu);


//Main
int main(){
	

	funcMENU(NewGame);
	funcMENU(PlayAgain);
	funcMENU(RestartGame);
	funcMENU(Instructions);

	//---------------------------------------------------------------------
	cout<<endl<<endl<<endl<<"Please Close Console Window"<<endl;
	cin.ignore('\n', 1024);
	return(0);
}

//Function itself
void funcMENU (EMenu eMenu){

	switch(eMenu){
	case NewGame:
		cout<<"New Game"<<endl;
		break;
	case PlayAgain:
		cout<<"Play Again"<<endl;
		break;
	case RestartGame:
		cout<<"Restart Game"<<endl;
		break;
	case Instructions:
		cout<<"Instructions"<<endl;
		break;

	}
}
This is the only other way I can get it to work with out a global.... but seams wrong as if i ever need to edit it I have to remember to edit it in two places not just one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// More Enum Tests
#include <iostream>
using namespace std;


//Function Deceleration
void funcMENU (int eMenu);


//Main
int main(){
	enum EMenu {NewGame, PlayAgain, RestartGame, Instructions};

	funcMENU(NewGame);
	funcMENU(PlayAgain);
	funcMENU(RestartGame);
	funcMENU(Instructions);

	//---------------------------------------------------------------------
	cout<<endl<<endl<<endl<<"Please Close Console Window"<<endl;
	cin.ignore('\n', 1024);
	return(0);
}

//Function itself
void funcMENU (int eMenu){
	enum EMenu {NewGame, PlayAgain, RestartGame, Instructions};
	switch(eMenu){
	case NewGame:
		cout<<"New Game"<<endl;
		break;
	case PlayAgain:
		cout<<"Play Again"<<endl;
		break;
	case RestartGame:
		cout<<"Restart Game"<<endl;
		break;
	case Instructions:
		cout<<"Instructions"<<endl;
		break;

	}
}


Is there a better way do doign this?
Last edited on
enum EMenu {NewGame, PlayAgain, RestartGame, Instructions};

This should be outside main. You aren't declaring a variable here, you are declaring a type.
Last edited on
Topic archived. No new replies allowed.