5 Replies - 201 Views - Last Post: 12 February 2014 - 06:16 AM Rate Topic: -----

#1 duanuys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-November 08

How can I make this code simpler?

Posted 11 February 2014 - 03:56 PM

I have this section of code for a game I am creating. It starts with the user pretty much inputing any variation of the word "Human" for it to be the same thing.. The only problem is I have a lot of ifs and else if statements to cover that, as you can imagine after a few different races, this can get a bit messy. I just learned about switches and enums through:
http://www.learncpp.com/

Can anyone help me on how I can condense the code to make it look nicer and still give the same effect. Thanks!

#include <iostream>
#include <string>

int nRace()
{
	using namespace std;

	enum Human
	{
		HUMAN_BRETON,
		HUMAN_IMPERIAL,
		HUMAN_NORD,
		HUMAN_REDGUARD,
	};

	string Race;

	cout << "Pick your race...\n\n" << endl;
	cout << "          *=====================*" << endl;
	cout << "          |                     | " << endl;
	cout << "          | Human / Elf / Beast | " << endl;
	cout << "          |                     | " << endl;
	cout << "          *=====================*\n\n" << endl;

	cin >> Race;
	
	if (Race == "human")
		cout << "You have picked Human";
	else if (Race == "Human")
		cout << "You have picked Human";
	else if (Race == "Hu")
		cout << "You have picked Human";
	else if (Race == "HU")
		cout << "You have picked Human";
	else if (Race == "hU")
		cout << "You have picked Human";
	else if (Race == "HUMAN")
		cout << "You have picked Human";
	else if (Race == "hu")
		cout << "You have picked Human";
	else
		cout << "You have NOT PICKED HUMAN";

	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: How can I make this code simpler?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8372
  • View blog
  • Posts: 31,116
  • Joined: 12-June 08

Re: How can I make this code simpler?

Posted 11 February 2014 - 04:01 PM

Simple - don't have 'Race' be a string. Have it be a number.

Quote

I have this section of code for a game I am creating.

08	    enum Human
09	    {
10	        HUMAN_BRETON,
11	        HUMAN_IMPERIAL,
12	        HUMAN_NORD,
13	        HUMAN_REDGUARD,
14	    };
15	 


o hai Bethesda Game Studios! I didn't see you there! ;)
Was This Post Helpful? 0
  • +
  • -

#3 duanuys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-November 08

Re: How can I make this code simpler?

Posted 11 February 2014 - 04:05 PM

That would require the user inputting a "Number", what if I like the feel of a string input. Is their a way I can keep that structure?
Was This Post Helpful? 0
  • +
  • -

#4 Black_Epiphany  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 60
  • Joined: 02-February 14

Re: How can I make this code simpler?

Posted 11 February 2014 - 04:09 PM

If you do want to use a string, just make sure to specifically tell the user how you want their choice to be expressed when inputting.

cout << "Select your race (Enter 'human', 'elf', 'orc')" << endl;
cin >> input;
//some code to validate input
//some code to interpret choice



You can use something like tolower on their choice to make sure it isn't case sensitive.
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: How can I make this code simpler?

Posted 11 February 2014 - 06:01 PM

All of your conditions have something in common, the string must start with the letters "hu". So simply your check and only compare the first two letters of the string. Of course this will input such as "hua;sldfjasl" to be considered as a human selection, but you can add extra conditions if you want to avoid this.

#include <iostream>
#include <string>
#include <algorithm>

int nRace()
{
	using namespace std;

	enum Human
	{
		HUMAN_BRETON,
		HUMAN_IMPERIAL,
		HUMAN_NORD,
		HUMAN_REDGUARD,
	};

	string Race;

	cout << "Pick your race...\n\n" << endl;
	cout << "          *=====================*" << endl;
	cout << "          |                     | " << endl;
	cout << "          | Human / Elf / Beast | " << endl;
	cout << "          |                     | " << endl;
	cout << "          *=====================*\n\n" << endl;

	cin >> Race;

     /* Convert input to lower case */
     std::transform(Race.begin(), Race.end(), race.begin(), std::tolower);

     /* Compare first two letters of string */
	if(Race.substr(0, 2) == "hu") {
         cout << "You have picked Human";

	

	return 0;
}



This post has been edited by jjl: 11 February 2014 - 06:03 PM

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: How can I make this code simpler?

Posted 12 February 2014 - 06:16 AM

Hmm... first, a helper function you will certainly want:
std::string toLower(const std::string &s) {
	std::string ls(s);
	std::transform(ls.begin(), ls.end(), ls.begin(), ::tolower);
	return ls;
}



With that in hand:
bool isHuman(const std::string &name) {
	std::string s = toLower(name);
	return (s=="human") || (s=="hu");
}

int nRace() {
	using namespace std;
	string race;

	cout << "Pick your race...\n\n" << endl;
	cout << "          *=====================*" << endl;
	cout << "          |                     | " << endl;
	cout << "          | Human / Elf / Beast | " << endl;
	cout << "          |                     | " << endl;
	cout << "          *=====================*\n\n" << endl;

	cin >> race;
	if (isHuman(race)) {
		cout << "You have picked Human";
	} else {
		cout << "You have NOT PICKED HUMAN";
	}
	return 0;
}




If the toLower doesn't make sense ( it's a bit of STL arcana ), then try rolling your own. Alternately, and to the same end, you could write a case insensitive string compare.

Hope this helps.

This post has been edited by baavgai: 12 February 2014 - 06:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1