getline help
Nov 22, 2011 at 8:48pm UTC
I really don't see any surrounding codes that could cause the redundant output. Check your keyboard to see if the Enter key is stuck.. XD
Nov 22, 2011 at 8:51pm UTC
Me either... your code is fine. It shouldn't repeat.
Last edited on Nov 22, 2011 at 8:51pm UTC
Nov 22, 2011 at 8:59pm UTC
If it was the keyboard, then why would it only do it for that instance, and everytime that instance occurs?
Nov 22, 2011 at 9:06pm UTC
Good Question. I'm not entirely sure why it's repeating like that. Did you paste all of your code? When I run your code you gave us, it works fine. It's probably something you did earlier....
Nov 22, 2011 at 9:17pm UTC
This is my whole code so far:
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
using namespace std;
int level = 1;
int Exp = 0;
int gold = 0;
class Hero
{
friend class Enemy;
friend int battlePhase(Hero* pHero);
public :
Hero(const string name);
void Name();
void Inn();
int m_hCHp;
int m_hCMp;
virtual int performAttack(Enemy* pEnemy);
string m_hName;
void levelUp();
protected :
int m_hHp;
int m_hMp;
int m_hAtk;
int m_hDef;
int m_hMAtk;
int m_hMDef;
int m_hSpd;
int m_hAcc;
int m_hLck;
virtual void assignStats() = 0;
};
Hero::Hero(const string name):
m_hName(name)
{}
void Hero::Name()
{
cout << m_hName;
}
void Hero::Inn()
{
m_hCHp = m_hHp;
m_hCMp = m_hMp;
cout << "Hp and Mp restored\n" ;
}
void Hero::levelUp()
{
cout << "Congratulations!! You leveled up!\n" ;
++level;
srand(clock() * (unsigned int )time(0));
int random = (rand() % (level + 1));
cout << m_hHp << " -> " ;
m_hHp += random;
cout << m_hHp << endl;
cout << m_hMp << " -> " ;
m_hMp += random;
cout << m_hMp << endl;
cout << m_hAtk << " -> " ;
m_hAtk += random;
cout << m_hAtk << endl;
cout << m_hDef << " -> " ;
m_hDef += random;
cout << m_hDef << endl;
cout << m_hMAtk << " -> " ;
m_hMAtk += random;
cout << m_hMAtk << endl;
cout << m_hMDef << " -> " ;
m_hMDef += random;
cout << m_hMDef << endl;
cout << m_hSpd << " -> " ;
m_hSpd += random;
cout << m_hSpd << endl;
}
class Soldier : public Hero
{
public :
Soldier(const string name);
protected :
virtual void assignStats();
};
Soldier::Soldier(const string name):
Hero(name)
{
assignStats();
}
void Soldier::assignStats()
{
m_hCHp = 25;
m_hHp = 25;
m_hCMp = 19;
m_hMp = 19;
m_hAtk = 15;
m_hDef = 15;
m_hMAtk = 3;
m_hMDef = 4;
m_hSpd = 9;
m_hAcc = 95;
m_hLck = 5;
}
class Fighter : public Hero
{
public :
Fighter(const string name);
protected :
virtual void assignStats();
};
Fighter::Fighter(const string name):
Hero(name)
{
assignStats();
}
void Fighter::assignStats()
{
m_hCHp = 29;
m_hHp = 29;
m_hCMp = 13;
m_hMp = 13;
m_hAtk = 17;
m_hDef = 10;
m_hMAtk = 2;
m_hMDef = 8;
m_hSpd = 11;
m_hAcc = 94;
m_hLck = 5;
}
class Spellcaster : public Hero
{
public :
Spellcaster(const string name);
protected :
virtual void assignStats();
};
Spellcaster::Spellcaster(const string name):
Hero(name)
{
assignStats();
}
void Spellcaster::assignStats()
{
m_hCHp = 19;
m_hHp = 19;
m_hCMp = 24;
m_hMp = 24;
m_hAtk = 5;
m_hDef = 6;
m_hMAtk = 17;
m_hMDef = 12;
m_hSpd = 7;
m_hAcc = 95;
m_hLck = 5;
}
class Theif : public Hero
{
public :
Theif(const string name);
protected :
virtual void assignStats();
};
Theif::Theif(const string name):
Hero(name)
{
assignStats();
}
void Theif::assignStats()
{
m_hCHp = 21;
m_hHp = 21;
m_hCMp = 18;
m_hMp = 18;
m_hAtk = 16;
m_hDef = 9;
m_hMAtk = 3;
m_hMDef = 4;
m_hSpd = 19;
m_hAcc = 94;
m_hLck = 6;
}
class Entertainer : public Hero
{
public :
Entertainer(const string name);
protected :
virtual void assignStats();
};
Entertainer::Entertainer(const string name):
Hero(name)
{
assignStats();
}
void Entertainer::assignStats()
{
m_hCHp = 20;
m_hHp = 20;
m_hCMp = 20;
m_hMp = 20;
m_hAtk = 10;
m_hDef = 10;
m_hMAtk = 10;
m_hMDef = 10;
m_hSpd = 10;
m_hAcc = 95;
m_hLck = 5;
}
class Skillmaster : public Hero
{
public :
Skillmaster(const string name);
protected :
virtual void assignStats();
};
Skillmaster::Skillmaster(const string name):
Hero(name)
{
assignStats();
}
void Skillmaster::assignStats()
{
m_hCHp = 26;
m_hHp = 26;
m_hCMp = 13;
m_hMp = 13;
m_hAtk = 12;
m_hDef = 15;
m_hMAtk = 4;
m_hMDef = 14;
m_hSpd = 6;
m_hAcc = 95;
m_hLck = 5;
}
class Enemy
{
friend int battlePhase(Hero* pHero);
public :
Enemy(const string name);
void Name();
int m_eCHp;
int m_eCMp;
int m_eExpDrop;
int m_eGoldDrop;
int performAttack(Hero* pHero);
int m_eDef;
string m_eName;
protected :
int m_eHp;
int m_eMp;
int m_eAtk;
int m_eMAtk;
int m_eMDef;
int m_eSpd;
int m_eAcc;
int m_eLck;
virtual void assignStats() = 0;
};
Enemy::Enemy(string name)
{
m_eName = name;
}
void Enemy::Name()
{
cout << Enemy::m_eName;
}
int Enemy::performAttack(Hero* pHero)
{
int atk = Enemy::m_eAtk;
int def = pHero->m_hDef;
srand(clock() * (unsigned int )time(0));
int randomizer = ((rand() % 51) + 75);
int damage = (randomizer * atk * 3 - randomizer * def * 3) / 100;
if (damage < 0)
{
damage = 0;
}
int lucky = (rand() % 100) + 1;
if (lucky <= m_eLck)
{
cout << "Critical Hit!\n" ;
damage *= 2;
}
else if (lucky >= m_eAcc)
{
cout << "Miss!" ;
damage = 0;
}
return damage;
}
class Mouse : public Enemy
{
public :
Mouse(const string name = "Mouse" );
protected :
virtual void assignStats();
};
Mouse::Mouse(const string name):
Enemy(name)
{
assignStats();
}
class Rat : public Enemy
{
public :
Rat(const string name = "Rat" );
protected :
virtual void assignStats();
};
Rat::Rat(const string name):
Enemy(name)
{
assignStats();
}
void Rat::assignStats()
{
m_eCHp = 28;
m_eHp = 28;
m_eCMp = 3;
m_eMp = 3;
m_eAtk = 21;
m_eDef = 5;
m_eMAtk = 7;
m_eMDef = 9;
m_eSpd = 15;
m_eAcc = 95;
m_eLck = 5;
m_eExpDrop = 2;
m_eGoldDrop = 4;
}
void Mouse::assignStats()
{
m_eCHp = 35;
m_eHp = 35;
m_eCMp = 3;
m_eMp = 3;
m_eAtk = 17;
m_eDef = 9;
m_eMAtk = 4;
m_eMDef = 11;
m_eSpd = 9;
m_eAcc = 95;
m_eLck = 5;
m_eExpDrop = 1;
m_eGoldDrop = 6;
}
int Hero::performAttack(Enemy* pEnemy1)
{
Enemy* pEnemy = pEnemy1;
int atk = Hero::m_hAtk;
int def = pEnemy->m_eDef;
srand(clock() * (unsigned int )time(0));
int randomizer = ((rand() % 51) + 75) / 10;
int damage = (randomizer * atk * 3 - randomizer * def * 3) / 10;
if (damage < 0)
{
damage = 0;
}
int lucky = (rand() % 100) + 1;
if (lucky <= m_hLck)
{
cout << "Critical Hit!\n" ;
damage *= 2;
}
else if (lucky >= m_hAcc)
{
cout << "Miss!" ;
damage = 0;
}
return damage;
}
int battlePhase(Hero* pHero);
int main()
{
cout << "Welcome to Battle System.\n\n" ;
cout << "What's your name?\n" ;
string inName;
getline (cin, inName);
cout << "What class will you be?\n" ;
cout << "\nFighter\nSoldier\nSpellcaster\nTheif\nEntertainer\nSkillmaster\n\n" ;
string inClass;
Hero* pHero1;
do
{
cin >> inClass;
if (inClass == "Soldier" )
{
pHero1 = new Soldier(inName);
}
else if (inClass == "Fighter" )
{
pHero1 = new Fighter(inName);
}
else if (inClass == "Spellcaster" )
{
pHero1 = new Spellcaster(inName);
}
else if (inClass == "Theif" )
{
pHero1 = new Theif(inName);
}
else if (inClass == "Entertainer" )
{
pHero1 = new Entertainer(inName);
}
else if (inClass == "Skillmaster" )
{
pHero1 = new Skillmaster(inName);
}
else
{
cout << "Try again.\n" ;
}
} while (inClass != "Fighter" && inClass != "Soldier" && inClass != "Spellcaster" && inClass != "Theif"
&& inClass != "Entertainer" && inClass != "Skillmaster" );
string selection;
while (selection != "Quit" )
{
if (pHero1->m_hCHp == 0)
{
break ;
}
cout << "What would you like to do?\n\n" ;
cout << "Battle\nInn\nQuit\n\n" ;
cin >> selection;
if (selection == "Battle" )
{
if (level == 1)
{
pHero1->m_hCHp = battlePhase(pHero1);
if (Exp >= 15)
{
pHero1->levelUp();
}
}
else
{
cout << "Can't let you do that, sonny!\n" ;
}
}
else if (selection == "Inn" )
{
pHero1->Inn();
}
else if (selection != "Quit" )
{
cout << "Try again.\n" ;
}
}
return 0;
}
Ill have to post the rest of it in another post cause its too long O.o
Nov 22, 2011 at 9:17pm UTC
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
int battlePhase(Hero* pHero)
{
srand(clock() * (unsigned int ) time (0));
int random = rand();
int encounter = (random % 2) + 1;
Enemy* pEnemy;
if (encounter == 1)
{
pEnemy = new Mouse();
}
else if (encounter == 2)
{
pEnemy = new Rat();
}
cout << "Wild " ;
pEnemy->Name();
cout << " appeared!\n" ;
while (pEnemy->m_eCHp > 0 && pHero->m_hCHp > 0)
{
cout << "What will you do?\n" ;
cout << "\nAttack\nRun Away\n\n" ;
string action;
do
{
getline(cin, action);
if (action == "Attack" || action == "Run Away" )
{
break ;
}
else
{
cout << "Nope. Try Again." << endl;
}
} while (action != "Attack" && action != "Run Away" );
if (action == "Run Away" )
{
cout << "You ran away.\n" ;
break ;
}
if (pHero->m_hSpd >= pEnemy->m_eSpd)
{
int damage = pHero->performAttack(pEnemy);
pEnemy->m_eCHp = pEnemy->m_eCHp - damage;
pHero->Name();
cout << " dealt " << damage;
cout << " damage to " ;
pEnemy->Name();
cout << endl;
if (pEnemy->m_eCHp <= 0)
{
break ;
}
damage = pEnemy->performAttack(pHero);
pHero->m_hCHp = pHero->m_hCHp - damage;
pEnemy->Name();
cout << " dealt " << damage;
cout << " damage to " ;
pHero->Name();
cout << endl;
if (pHero->m_hCHp <=0)
{
break ;
}
cout << "\nYour Hp: " << pHero->m_hCHp;
cout << "\nEnemy Hp: " << pEnemy->m_eCHp << endl;
}
else if (pHero->m_hSpd < pEnemy->m_eSpd)
{
int damage = pEnemy->performAttack(pHero);
pHero->m_hCHp = pHero->m_hCHp - damage;
pEnemy->Name();
cout << " dealt " << damage;
cout << " damage to " ;
pHero->Name();
cout << endl;
if (pHero->m_hCHp <=0)
{
break ;
}
damage = pHero->performAttack(pEnemy);
pEnemy->m_eCHp = pEnemy->m_eCHp - damage;
pHero->Name();
cout << " dealt " << damage;
cout << " damage to " ;
pEnemy->Name();
cout << endl;
if (pEnemy->m_eCHp <= 0)
{
break ;
}
cout << "\nYour Hp: " << pHero->m_hCHp;
cout << "\nEnemy Hp: " << pEnemy->m_eCHp << endl;
}
}
if (pEnemy->m_eCHp <= 0)
{
cout << pEnemy->m_eName << " has been defeated!\n" ;
cout << "You gained " << pEnemy->m_eExpDrop << " Exp!\n" ;
Exp += pEnemy->m_eExpDrop;
cout << "You gained " << pEnemy->m_eGoldDrop << " Gold!\n" ;
gold += pEnemy->m_eGoldDrop;
}
else if (pHero->m_hCHp <= 0)
{
cout << "You died\n" ;
}
else
{
cout << endl;
}
return pHero->m_hCHp;
}
Nov 22, 2011 at 9:21pm UTC
Its in the second post where the problem is occuring
Nov 22, 2011 at 9:47pm UTC
Did anyone see anything wrong with it?
Topic archived. No new replies allowed.