Error after adding while do loop and isalpha

Here's the program:
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
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;
	}	
	else if (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";
		else if (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   
---------------------------


So, what do I do to get it to work properly?
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.
I don't understand, is this supposed to look like this?
int loss;
1
2
3
4
5

while (isalpha(loss))
std::cout << "Type in the amount of health " << heroname << " will lose  =  ";
	std::cin >> loss;	

If yes, it adds another error.
Last edited on
bump.
bump.
The problem is still there, still can't solve it.
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.

Edit: Here, about half way down. http://msdn.microsoft.com/en-us/library/xt82b8z8(v=vs.71).aspx

Last edited on
isalpha() is intended to test a char to see if it's a letter. I have no idea why it takes an int rather than a char, but you use it with chars.

1
2
3
4
isalpha('a') -> not 0, isdigit('a') -> 0, ispunct('a') -> 0, etc
isalpha('2') -> 0, isdigit('2') -> not 0, ispunct('2') -> 0, etc
isalpha(':') -> 0, isdigit(':') -> 0, ispunct(':') -> not 0, etc
etc


http://www.cplusplus.com/reference/clibrary/cctype/isalpha/

An int has to be a number, so your loop doesn't make sense to start with. Did you mean to get a string and check if it was a number?

1
2
3
4
5
6
string loss;
do {
    std::cout << "Type in the amount of health " << heroname << " will lose  =  ";
    std::cin >> loss;	
}
while ( !isnumeric(loss) );


where

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// check that all chars in string are numbers
bool isnumeric(const char* value)
{
    if((value==NULL) || (strlen(value)==0))
        return false;

    for(int i=0; i<strlen(value); i++)
    {
        if (!isdigit(value[i]))
            return false;
    }

    return true;
}


And then you have to convert the numeric string to an int.

Or you could test cin, after trying to extract and int, to see if it was given an non-numeric string by checking its state using istream::bad()
http://www.cplusplus.com/reference/iostream/istream/
http://www.cplusplus.com/reference/iostream/ios/bad/

Andy
Last edited on
I know what isalpha does, I just forgot to change int to char, thanks.
Topic archived. No new replies allowed.