【问题标题】:isalpha causing a "Debug Assertion Failed" in c++isalpha 在 C++ 中导致“调试断言失败”
【发布时间】:2018-09-05 20:56:49
【问题描述】:

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

【问题讨论】:

  • 你给程序提供什么输入?
  • 请将问题 edit 1. 指明您提供的输入,以及 2. 包括错误消息中显示 which 调试断言失败的部分。
  • 如果只输入 3 个字符会怎样?你为什么一直循环到81?看到这个问题了吗?为什么不使用std::string,这些字符串大小问题会自动处理?
  • 你永远不应该信任你的用户。验证你得到的输入。
  • int isalpha( int ch ); 采用int,因此会发生扩大。如果char 已签名且strChar 为负,则将导致未定义行为。 “如果 ch 的值不能表示为 unsigned char 或不等于 EOF,则行为未定义。” en.cppreference.com/w/cpp/string/byte/isalpha

标签: c++ assertion isalpha


【解决方案1】:
int ConsCount(char *string, const int size)
{
    int consCount = 0;

    char* begin = string;
    for (char* itr = begin; *itr != '\0'; ++itr)
    {
        if (isalpha(*itr)) {
            char ch = toupper(*itr);

            switch(ch) {
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                    break; // If ch is any of 'A', 'E', 'I', 'O', 'U'
                default:
                    ++consCount;
           }
        }
    }

    return consCount;

试试这个

}

【讨论】:

  • 为什么要复制答案?
【解决方案2】:

函数ConsCount(char* string, const int size)应该是这样的:

int ConsCount(char *string, const int size)
{
    int consCount = 0;

    char* begin = string;
    for (char* itr = begin; *itr != '\0'; ++itr)
    {
        if (isalpha(*itr)) {
            char ch = toupper(*itr);

            switch(ch) {
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                    break; // If ch is any of 'A', 'E', 'I', 'O', 'U'
                default:
                    ++consCount;
            }
        }
    }

    return consCount;
}

如您所见,我将 if 语句替换为 switch 以获得更好的可读性,并使用 char* 作为迭代器来迭代字符串。您可以删除代码中未使用的参数int size

我还建议您使用std::string 作为安全代码。它还为您提供了一个 iterator 类来迭代 std::string

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多