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!
#include<iostream>
#include<cctype>
int wordCounter(char array[],constint space);
usingnamespace std;
int main()
{
constint 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[],constint space )
{
int count,spaceCount=0;
for (count=0;count<space-1; count++)
{
if (isspace(array[count]))
spaceCount++;
}
return spaceCount;
}
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.
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.