Hi, I need a way to match a word to a string regardless of the spelling.
so a user can input a word and the program will read it and do what they asked.
1 2 3 4 5 6 7 8
|
//Such as
cout << "Where do you want to go?" << endl;
cin >> string;
if( string == "North" || string == "north" || string == "n" )
{ goto northRoom; }
else if( string == "South" || string == "south" || string == "s" )
{ goto southRoom; }
|
I started by doing that, and it worked fine, but it takes too much screen space
and time to type.
then i switched to the "switch" function
1 2 3 4 5 6 7 8 9 10
|
cout << "Which way would you like to go?" << endl;
cin >> string;
switch( string )
{
case 'n' :
{ goto northRoom; }
case 's' :
{ goto southRoom; }
}
|
and that works, but only if you input a "n" or a "s"
So I googled around a bit and discovered the find() function
and got an idea
1 2 3 4 5 6 7 8 9 10
|
string options = "North north n South south s";
string str;
cout << "Which way would you like to go?" << endl;
cin >> string;
found = options.find(string);
if( ( found > 0 ) && ( found < 13 ) )
{ goto northRoom; }
|
But it doesn't work, and I'm pretty sure i understand why, but I am looking for
a way to do something like these that will just match an input to a string,
and send you to a room if it matches the appropriate rooms direction
1 2 3 4 5 6 7
|
//Like
directions = "north east south west";
cin >> string;
if( string == a direction )
{ goto the room in that direction }
|
Also I Am trying to create an advanced combat system and it works, as far as letting the player damage the opponent, but the opponent does 0 damage.
ANY positive/productive feedback is appreciated.
Here's My Code for it:
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
//Advanced Combat - Working. (but not complete)
void advCombat(string opponent)
{
attacker = opponent; // Opponents Name
opp = level * 5; // Opponents Level say level 5---> Enemy level is 25
enemyHp = level * opp; // Players Level * Attackers Level and health is 125
enemyHpMax = level * opp; // Opponents Max Health say level 10--> Enemy lvl 50 and health is 500
enemyDmg = ( ( level / opp ) * str ) * 10;
cout << attacker << " is level " << opp << endl << endl;
//Attack System ( With/Without Sword )
do{
cout << "What now?";
cin >> answer;
if( sword == false )
{
srand(time(0)); int n = rand() % 10+1;
if( n > 4 )
{ //Essentially you do 8.56% of the Opponents health for damage
double dmg = ( opp / 35.0 ) * str; // At level 5 You do (.714 * str) dmg
enemyHp -= dmg; // So You'd do 10.7 Damage
cout << "You inflicted " << dmg << " to " << attacker // out of 125 health to the attacker
<< endl << " has " << enemyHp << "/" << enemyHpMax << endl; // and 107 if you have the sword
} // but there is a greater chance
else if( n < 4 ) // that you will miss if you are
{ // weilding the mighty sword
cout << "Oh no! You've Missed!"; cin.ignore(); cin.get();
}
}
else if( sword == true )
{
srand(time(0)); int n = rand() % 10+1;
if( n > 6 )
{
double dmg = ( ( opp / 35.0 ) * str ) * 10;
enemyHp -= dmg;
cout << "You dealt " << dmg << " damage to " << attacker
<< endl << attacker << " has " << enemyHp << "/" << enemyHpMax << endl;
}
else if( n < 6 )
{
cout << "Oh no! You've Missed!"; cin.ignore(); cin.get();
}
}
cout << attacker << " retaliated."; cin.ignore(); cin.get();
//Defense system
if( cloak == true )
{
srand(time(0)); int n = rand() % 20+1;
//Attacker Missed
if( n < 4 )
{
cout << endl << endl << "You button your Trench coat in a flash, and Vanish!"
<< endl << attacker << " Missed!"; cin.ignore(); cin.get();
}
//Attacker Hit
else if( n > 4 )
{ srand(time(0)); int n = rand() % 1000000+1;
//RANDOM CLOAK SHREDDING!! WOO.
if( n > 999997 && n < 999999 ) { cout << "Oh Dear, you're Cloak has been shredded to bits" << endl; }
enemyDmg = ( ( level / opp ) * str ) * 10;
cout << "You were too slow, and " << attacker << " Thwakk'd you."
<< endl << attacker << " dealt " << enemyDmg << " damage to you!" << endl;
health -= enemyDmg;
cout << health << "/" << healthMax << " remaining."; cin.ignore(); cin.get();
}
}
else
{ srand(time(0)); int n = rand() % 10+1;
//Attacker Hit
if( n < 4 )
{
enemyDmg = ( ( level / opp ) * str ) * 10;
health -= enemyDmg;
cout << "You've Been Hit! "
<< endl << attacker << " dealt " << enemyDmg << " damage to you!"; cin.ignore(); cin.get();
}
else if( n > 4 )
{
cout << "Hoora! " << attacker << " missed! (Let's celebrate! Champaigne For all)";
}
}
if( enemyHp < 0 ) { cout << "You killed " << attacker << "!"; cin.ignore(); cin.get(); }
}while( health > 0 && enemyHp > 0 );
}
|