Issue during debug





Hello! was working on some home work and ran into issue that I don't quite understand and was hoping someone can clue me into what is going on with the error and point me in the right direction.

Anyhow the program I am trying to write accepts a sentence and a function within the program and it is suppose to test a sentence from user input to determine how many spaces and return that value.

Here is the code I have so far. Thanks in advance!



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

#include<iostream>
#include<cctype>
int wordCounter(char array[],const int space);
using namespace std;

int main()
{
	const int size=51;
	char word[size];
	


	cout<<"             Word Counter\n\n\n";
	cout<<"Please enter a 50 character sentence and Word Counter\nwill give you the number of words in the sentence.\n";

	cin.getline(word, size);
	cout<<wordCounter(&word[size], size);


cin.ignore();
cin.get();
return 0;
}
int wordCounter(char array[],const int space )
{
	int count,spaceCount=0;
	for (count=0;count<space-1; count++)
		{
		
		if (isspace(array[count]))
			spaceCount++;
	    }
	 

return spaceCount;
}
If i'm not mistaken, you don't need to send the size of the array, or the address, since arrays are already pointers to the start of memory blocks.
The way you used word on line 17 is the way you should be using word on line 18.

&word[size] is the address of the 52nd element of word (IOW, the address of an element that doesn't exist.)


Thanks for the Feedback thus far, I have corrected the issue with line 18. However this is the error I run into.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
* Purpose:
*       This function is called by character testing functions in debug
*       versions. This function test for validation of c as character.
*       For improvement in performance, it is not used in non-debug
*       version.  It is available in the static single-thread non-debug
*       build, though, just in case C code that includes ctype.h is compiled
*       /D_DEBUG /ML.
*
*******************************************************************************/

#if defined (_DEBUG)
extern "C" int __cdecl _chvalidator(
        int c,
        int mask
        )
{
        _ASSERTE((unsigned)(c + 1) <= 256);



my debugger points to line 17 and I have no idea as to what it is referring too.
Does changing line 31 to if ( isspace(static_cast<unsigned char>(array[count])) ) keep that from happening?
Thank you, that did work.

Is there any reference material I can read up on that explains why that happened? or is this something I will eventually run into while I learn more c++?
The isxxx functions only work for values in the range of -1 (eof) to 255.

According to the standard, char can be equivalent to unsigned char or signed char. It would appear that in your environment it is the signed version. Casting the character to unsigned char before passing it to those functions prevents feeding them values they aren't expecting.
Ahh that makes perfect sense now. basically its testing the ascii table?
Topic archived. No new replies allowed.