c++ - isalpha 在 C++ 中导致 "Debug Assertion Failed"

标签 c++ assertion isalpha

我有一个简短的程序,旨在通过首先测试数组中的字符是否为字母字符(跳过任何空格或标点符号)来计算字符串中辅音的数量。我的“if (isalpha(strChar))”代码行不断出现调试断言失败。

“strChar”是一个在 for 循环中被赋予 char 值的变量

抱歉,如果这是一个补救性问题,但我不确定我哪里出错了。在此先感谢您的帮助!

#include <iostream>
#include <cctype>
using namespace std;
int ConsCount(char *string, const int size);


int main()
{
    const int SIZE = 81; //Size of array
    char iString[SIZE]; //variable to store the user inputted string

    cout << "Please enter a string of no more than " << SIZE - 1 << " characters:" << endl;
    cin.getline(iString, SIZE);

    cout << "The total number of consonants is " << ConsCount(iString, SIZE) << endl;
}

int ConsCount(char *string, const int size)
{
    int count;
    int totalCons = 0;
    char strChar;

    for (count = 0; count < size; count++)
    {
        strChar = string[count];
        if (isalpha(strChar))
            if (toupper(strChar) != 'A' || toupper(strChar) != 'E' || toupper(strChar) != 'I' || toupper(strChar) != 'O' || toupper(strChar) != 'U')
            totalCons++;
    }
    return totalCons;
}

最佳答案

我想问题是您总是循环遍历 81 个字符,即使输入的字符较少。这导致一些随机数据被馈送到 isalpha()

无论如何,我会更改代码以使用 std::string 而不是 char iString[SIZE] 来获取输入文本的实际长度。

关于c++ - isalpha 在 C++ 中导致 "Debug Assertion Failed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49513059/

相关文章:

检查 `isalpha` 的返回值

c++ - std::string 复制构造函数在 GCC 4.1.2 中不是很深?

C++ 标准 : is there a result object?

c++ - OpenCV 相机校准,断言失败 <ni> 0 && ni==ni1>

c++ - OpenCV calibrateCamera 断言失败(ni == ni1)

c++ - 阿尔法()?或其他...调试断言失败,circlemud 游戏

c++ - 为什么我在打印值时得到垃圾值?

c++ - 如何将行号添加到 QTreeView?

java - jUnit中的CollectionAssert?

python - 如何仅检查 isalpha() 字符串的前 5 个字符?