문제

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.

다른 팁

The reason why you are getting this error message is because isspace() accepts an integer value (int) but operates on characters (typically of type char). You must be passing an uninitialized negative value which is outside of the domain handled by isspace(). The value passed is incorrect and the ispace() implementation is gently informing you of this bug in your software. The library must have been compiled on a machine where there was an f:\ drive. The implementation does have more than 56 lines of code.

Also...

Use wordCount(strlen(wordArray), wordArray) instead of passing SIZE. Otherwise you will be reading uninitialized values which is bad.

Instead of run = false use break and replace the while with while(1). Also this loop very likely doesn't do what you think it does. You are merely overwriting the first byte with the second one. You probably want to shift all the characters to the left.

In your last loop, iterate until you hit a null byte ('\0') instead of up to size as this is also incorrect. Note that the string may now be smaller than size; perhaps size should not be a parameter at all...

Given these problems, here is an alternative way to approach the problem which doesn't require modifying the original string.

int count_words(const char *s) {
    int count = 0;
    bool in_word = false;
    while (*s != '\0') {
        if (isspace(*s)) {
            in_word = false;
        }
        else if (in_word == false) {
                count += 1;
                in_word = true;
        }
        ++s;
    }
    return count;
}

This assertion error comes from inside the C runtime library. Presumably it was built by someone who does have an F drive. The line number is in that source code.

The reason for the assertion is that you are trying to test character type for something that is not a valid character. The most likely candidate is the null end of string terminator.

You should not pass in the size, but use strlen() to find it. Then you will not run into the null.

for (int i = 0; i < strlen(wordArray); i++) { ...

Each of the 3 loops is buggy, and will not do what the comments say. You should fix the above 2 problems first, then see if you can debug the code.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top