string heroname;
std::cout << "Type in the name of the Hero = ";
getline(cin, heroname);
string monster;
if (randn == 0)
{
monster ="Gnome";
}
else
{
monster = "Bear";
}
std::cout << "You will fight against a " << monster << "\n";
int hhealth;
std::cout << heroname <<" has 100 health points by default.\n";
hhealth = 100;
int loss;
do {
std::cout << "Type in the amount of health " << heroname << " will lose = ";
std::cin >> loss;
}
while (isalpha(loss));
int monsterhealth;
if (randn = 0)
{
monsterhealth = 10;
}
elseif (randn = 1)
{
monsterhealth = 60;
}
std::cout << monster << " has " << monsterhealth << " health points\n";
int monsterloss;
cout << "Type in the amount of health " << monster << " will lose = ";
cin >> monsterloss;
int mhr = monsterhealth - monsterloss;
int hhr = hhealth - loss;
if (mhr <= 0 && hhr <= 0)
cout << "Stalemate!\n";
elseif (mhr <= 0)
cout << heroname <<" wins! ";
else cout << monster <<" wins! ";
lossfunc(hhealth, loss, heroname, monster, monsterhealth, monsterloss);
_getch();
}
I think the problem is with the int loss part.
I added a do while loop, then added a restriction to isalpha(loss). I wanted the program to disallow accepting non-numbers.
If I input a number it's okay, but when I input a letter, this happens:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!
Program: ...ter\documents\visual studio 2010\Projects\FRPG\Debug\FRPG.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\isctype.c
Line: 56
Expression: (unsigned)(c + 1) <= 256
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Cancel Retry Ignore
---------------------------
Isalpha checks for letters if you are inputting a letter you will get an infinite loop because there is no code within that loop. If you are trying to disallow non numbers then you should re prompt after they enter a non number.
Dude, what? I don't know what are you talking about.
There is code withing that loop,
std::cout << "Type in the amount of health " << heroname << " will lose = ";
std::cin >> loss;
for example.
I'm a beginner so I don't know what are you talking about, sorry.
This worked a while ago, then it stopped working, if it helps.
Using a do-while loop will run that section of code no matter what, regardless of what they enter. It checks the test statement after running the body of the loop. Try using a while loop instead see if the results are better.
The problem is that if you use the isalpha() function with Microsoft's debug runtime, when the input is not a letter, it does this. Don't use the Microsoft debug runtime if you want to be able to enter a number (which clearly you do) without having to push Ignore on this box that pops up.