我有一个简短的程序,旨在通过首先测试数组中的字符是否为字母字符(跳过任何空格或标点符号)来计算字符串中辅音的数量。我的“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/