Trying to do an assignment when I try to compile the bottom code and I get the following debug assertion failed stub:
File: f:\dd\vctools\crt\crtw32\convert\istype.c
Line: 56
Experession c >= -1 && c <= 255
Several problems seem apparant with this error message. I don't even have an f drive or directory, and unless the line is being counted in the isctype.c program I don't have 56 lines in my code.
The objective is to count the amount of words that the user enters. Check for spaces beforehand as well as null terminating characters.
The code below has been fixed according to comments from other users
#include <stdafx.h>
#include <iostream>
#include <string.h>
#include <cctype>
using namespace std;
int wordCount(int size, char wordArray[]);
int main(){
const int SIZE = 100;
char wordArray[SIZE];
cout << "What is the string you wish to enter? ";
cin.getline(wordArray, sizeof(wordArray));
cout << "The number of words entered is: " << wordCount(strlen(wordArray), wordArray) << endl;
}
int wordCount(int size, char wordArray[]){
int charCount = 0, wordCount = 0;
//counts the number of words in the entered string
for (int i = 0; wordArray[i] != '\0'; i++){
if (isalnum(wordArray[i])){
charCount++;
if (isspace(wordArray[i + 1])){
charCount = 0;
wordCount++;
}
}
}
return wordCount;
}
This code:
if (isspace(wordArray[i] && isspace(wordArray[i + 1]))){
has a couple of problems. You have your brackets in the wrong spot, so you are calling issspace
with a boolean parameter.
Also, you should get the size of the string from strlen, at the moment you are looping past the end of the string. The assert may be happening because you are passing an invalid char value to isspace
(e.g a negative number).
Edit: Also note the next line:
wordArray[i] = wordArray[i++];
is not going to do what you want. You want to move the rest of the string back one, not just copy one character to another.