Jump to content
  • Advertisement
Sign in to follow this  
Azteroth

just need a little help

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm reading beginning c++ game programming and I kinda caught a small snag on the exercises at the end of chapter 2, the first exercise says to use an enumeration to rewrite the menu chooser program except I can't get my prog to work right.
// Menu Chooser 
// Demonstrates the use of an enumeration for the difficulty level

#include <iostream>
#include <string>
using namespace std;



enum diff
{
	easy = 1,
	normal,
	hard,
	leave
};

int diffLevel(const diff& difLvl)
{
	switch(difLvl)
	{
	case easy:
		cout << "\nYou chose easy as your difficulty level" << endl;
		break;
	case normal:
		cout << "\nYou chose normal as your difficulty level" << endl;
		break;
	case hard:
		cout << "\nYou chose hard as your difficulty level" << endl;
	case leave:
		cout << "\nGood Bye!" << endl;
		break;
	default:
		cout << "\nFailed! HAHAHAHAHA Idiot illegal selection" << endl;
	}

	return 0;
}

int main()
{	
	int n;

	cout << "\tMenu Chooser 2.0\n\n";

	cout << "1 - Easy" << endl;
	cout << "2 - Normal" << endl;
	cout << "3 - Hard" << endl;
	cout << "4 - leave" << endl;

	cout << "\nEnter your difficulty: ";
	cin >> n;

	int diffLevel(difLvl = n);
	
	

	cout << "\n\nPress the ENTER key to continue...";
	cin.ignore(cin.rdbuf()->in_avail()+1);

	return 0;
}
now I don't know why but it won't show my cout's to their selections thanks ahead, Azteroth [edit: added code tags -SiCrane]

Share this post


Link to post
Share on other sites
Advertisement
int diffLevel(difLvl = n);

This line is your error. You're trying to call a function, but your syntax is wrong (none of my compilers would even compile it). To start with, C++ only supports positional parameters but you seem to be trying to use named parameters. Don't assign the value you want to pass to the function to the parameter name, just list the parameters in order, i.e.:
void function(int integer, char character)
{
}

int main()
{
// incorrect, C++ does not support named parameters
// function(integer= 7, character= 'a');

// correct, parameters passed by position
// 7 maps to the first listed parameter, "integer"
// 'a' maps to the second listed parameter, "character"
function(7, 'a');

// legal but might not be what you wanted to do, parameters passed by position
// 'a' maps to the first listed parameter, "integer"
// 7 maps to the second listed parameter, "character"
function('a', 7);
}

Secondly, you should not list the return type of the function. If you want to use the return value of the function you should either store it in a variable of appropriate type, or pass it to a function:
int plusOne(int value)
{
return value + 1;
}

int main()
{
// ignore return value
plusOne(7);

// store return value
// type does not have to match, just be convertable
float sevenPlusOne = plusOne(7);

// pass return value to function
std::cout << "7 + 1 = " << plusOne(7) << '\n';
}

Enigma

Share this post


Link to post
Share on other sites
thanks Enigma that was a quick response.

ok here's what I've done fixed that one error from earlier but also used your comments so here the new error:


// Menu Chooser
// Demonstrates the use of an enumeration for the difficulty level

#include <iostream>
#include <string>
using namespace std;



enum diff
{
easy = 1,
normal,
hard,
leave
};

void diffLevel(const diff& difLvl)
{
switch(difLvl)
{
case easy:
cout << "\nYou chose easy as your difficulty level" << endl;
break;
case normal:
cout << "\nYou chose normal as your difficulty level" << endl;
break;
case hard:
cout << "\nYou chose hard as your difficulty level" << endl;
case leave:
cout << "\nGood Bye!" << endl;
break;
default:
cout << "\nFailed! HAHAHAHAHA Idiot illegal selection" << endl;
}
}

int main()
{
int n;

cout << "\tMenu Chooser 2.0\n\n";

cout << "1 - Easy" << endl;
cout << "2 - Normal" << endl;
cout << "3 - Hard" << endl;
cout << "4 - leave" << endl;

cout << "\nEnter your difficulty: ";
cin >> n;

diffLevel(n);



cout << "\n\nPress the ENTER key to continue...";
cin.ignore(cin.rdbuf()->in_avail()+1);

return 0;
}


c:\game stuff\programming\bcppgp\chapter 2\Menu Chooser 2.0\main.cpp(52): error C2664: 'diffLevel' : cannot convert parameter 1 from 'int' to 'const diff &'


how do I get this to convert? I've tried changing const to int to no avail

thanks again
Azteroth

[Edited by - Azteroth on August 19, 2005 4:21:45 PM]

Share this post


Link to post
Share on other sites
First, an enum is nothing more than an integer with a meaning. You should normally pass its value in a function the same way you pass an integer. For example:

int diffLevel( diff lvl )
{
...
}

This has nothing to do with your error, but just thought I'd tell you.

Changes that are relevant are these:



// Your enum should explicitly define their values if you want to be sure
// what they are, like:
enum diff
{
easy = 1,
normal = 2,
hard = 3,
leave = 4
};

// ...

int diffLevel( diff difLvl )
{
// ...
default:
// you should not need a default as an enum doesn't have other values than
// the ones that are defined
// ...
}

// ...

// diffLevel ( n );
// Here the compiler can't convert automatically from an int to a diff enum
// value because it can't be sure that the range is valid, so you should do this
// instead

if( ( n >= easy ) && ( n <= leave ) )
{
diffLevel( (diff)n );
}
else
{
// display an error message about invalid input, or allow the user to try
// again
}

//...




Hope this helps.

Share this post


Link to post
Share on other sites
Persil,

Many thanks that did got it and thank you for that bit of info anything and everthing helps right now.

Again thanks to both you and Enigma for your help in getting me past this snag

Azteroth

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!