Jan 20, 2015 at 12:14am UTC
This is part of my program i'm creating, i have a bug I can't figure out. When I go to the "Create" part of the if else statement, and enter a skill, then a amount to increase, it increases correctly but then it repeats the whole while loop, outputs the very last else statement, then repeats "please pick a stat to increase". I can't figure out what is going on.
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
void gameStart()
{
std::cout << "Great! Time to pick your class or create a custom one!" << std::endl;
std::cout << "\nPick\nCreate" << std::endl;
bool choiceMade = false ;
while ( !choiceMade )
{
std::string choice = get_input();
if ( choice == "PICK" )
{
std::cout << "Wizard 25 health - 55 attack - 10 defense - 0 strength - 10 agility" << std::endl;
std::cout << "Archer 30 health - 35 attack - 15 defense - 0 strength - 20 agility" << std::endl;
std::cout << "Warrior 40 health - 20 attack - 20 defense - 15 strength - 5 agility" << std::endl;
std::cout << "Brute 50 health - 10 attack - 35 defense - 5 strength - 0 agility" << std::endl;
choice = get_input();
if ( choice == "WIZARD" )
{
hero.increase_agility(10);
hero.increase_defense(10);
hero.increase_attack(55);
hero.increase_health(25);
choiceMade = true ;
}
else if ( choice == "ARCHER" )
{
hero.increase_agility(20);
hero.increase_defense(15);
hero.increase_attack(35);
hero.increase_health(30);
choiceMade = true ;
}
else if ( choice == "WARRIOR" )
{
hero.increase_agility(5);
hero.increase_strength(15);
hero.increase_defense(20);
hero.increase_agility(20);
hero.increase_health(40);
choiceMade = true ;
}
else if ( choice == "BRUTE" )
{
hero.increase_strength(5);
hero.increase_defense(35);
hero.increase_attack(10);
hero.increase_health(50);
choiceMade = true ;
}
else
{
std::cout << "Valid choices are wizard, archer, warrior, and brute." << std::endl;
}
}
else if ( choice == "CREATE" )
{
while ( hero.get_unassigned_points() > 0 )
{
std::cout << "Please pick a skill you wish to increase." << std::endl;
std::cout << "\nHealth\nAttack\nDefense\nStrength\nAgility" << std::endl;
choice = get_input();
if ( choice == "HEALTH" )
{
std::cout << "How many points do you wish to assign to Health?" << std::endl;
int amount;
std::cin >> amount;
if ( std::cin.fail() )
{
std::cin.clear();
std::cin.ignore(1000, '\n' );
std::cout << "Unrecognized number." << std::endl;
}
else
{
hero.increase_health(amount);
}
}
else if ( choice == "ATTACK" )
{
std::cout << "How many points do you wish to assign to Attack?" << std::endl;
int amount;
std::cin >> amount;
if ( std::cin.fail() )
{
std::cin.clear();
std::cin.ignore(1000, '\n' );
std::cout << "Unrecognized number." << std::endl;
}
else
{
hero.increase_attack(amount);
}
}
else if ( choice == "STRENGTH" )
{
std::cout << "How many points do you wish to assign to Strength?" << std::endl;
int amount;
std::cin >> amount;
if ( std::cin.fail() )
{
std::cin.clear();
std::cin.ignore(1000, '\n' );
std::cout << "Unrecognized number." << std::endl;
}
else
{
hero.increase_strength(amount);
}
}
else if ( choice == "DEFENSE" )
{
std::cout << "How many points do you wish to assign to Defense?" << std::endl;
int amount;
std::cin >> amount;
if ( std::cin.fail() )
{
std::cin.clear();
std::cin.ignore(1000, '\n' );
std::cout << "Unrecognized number." << std::endl;
}
else
{
hero.increase_defense(amount);
}
}
else if ( choice == "AGILITY" )
{
std::cout << "How many points do you wish to assign to Agility?" << std::endl;
int amount;
std::cin >> amount;
if ( std::cin.fail() )
{
std::cin.clear();
std::cin.ignore(1000, '\n' );
std::cout << "Unrecognized number." << std::endl;
}
else
{
hero.increase_agility(amount);
}
}
else
{
std::cout << "Unrecognized input" << std::endl;
}
}
}
else
{
std::cout << "Unrecognized command." << std::endl;
}
}
}
Last edited on Jan 20, 2015 at 12:15am UTC
Jan 20, 2015 at 12:32am UTC
Possible reason may be, that while you increase the skill, you don't decrease the remaining unassigned_points. Maybe add hero.decrease_unassigned_points(amount) in the increase skill section.
Jan 20, 2015 at 12:47am UTC
simplify your code, 154 lines is excessive to look at.
get a debugger and perform an step-by-step run and take note of the path followed.
¿are you using getline()?
Jan 20, 2015 at 12:47am UTC
! is a operator that basically means opposite of whatever comes after, in this case, opposite of false is true, therefore it will execute while choiceMade is false, when it is changed to true, then the loop will equal false and cease to execute.
Jan 20, 2015 at 1:13am UTC
perhaps you have left a '\n'
in the input buffer and your `get_input()' function is returning an empty string.
Jan 20, 2015 at 1:20am UTC
That was it, I changes it to std::cin >> input. Thank you for that... lol!