c++ - 使用 ispunct 的函数断言失败

标签 c++ arrays function visual-studio-2012 ctype

我在构建“ReplacePuncsWithBlanks”函数后遇到断言失败。和我在数组首尾插入空格有什么关系吗?

 #include <iostream>
    #include <ctype.h>

    using namespace std;

    const int SIZE = 100;

    void GetString(char StringArray[]);
    void CopyString(char StringArray[], char CopiedArray[]);
    void MakeUpper(char CopiedArray[]);
    void InsertBlanks(char CopiedArray[]);
    void ReplacePuncsWithBlanks(char CopiedArray[]);
    void DisplayString(char StringArray[], char CopiedArray[]);   

    int main()
    {
        char StringEntered[SIZE];
        char CopiedString[SIZE];

        GetString(StringEntered);
        CopyString(StringEntered, CopiedString);
        MakeUpper(CopiedString);
        InsertBlanks(CopiedString);
        ReplacePuncsWithBlanks(CopiedString);
        DisplayString(StringEntered, CopiedString);

    cout<<"Hit Enter.\n";
    cin.ignore();

    }
/***************************************GetString********************************************
*   Action:         Prompts user to enter some text.                                        *
*                                                                                           *
*   Parameters:                                                                             *
*       IN:                                                                                 *
*                                                                                           *
*       OUT:        StringArray which is the array holding the original text inputted by    *
*                   the user                                                                *
*                                                                                           *
*   Returns:        Nothing.                                                                *
*                                                                                           *
*   Precondition:   StringArray points to StringEntered array in Main.                      *
*********************************************************************************************/
void GetString(char StringArray[])
{
    cout<<"Please enter a sentence:  ";
    cin.getline(StringArray, SIZE);
}


/***************************************CopyString********************************************
*   Action:         Copies data from one array (StringArray) to another (CopiedArray).      *
*                                                                                           *
*   Parameters:                                                                             *
*       IN:         StringArray which has existing data.                                    *
*                                                                                           *
*       OUT:        CopiedArray which will be a copy of StringArray.                        *
*                                                                                           *
*   Returns:        Nothing.                                                                *
*                                                                                           *
*   Precondition:   StringArray points to StringEntered array in Main.                      *
*                   CopiedArray points to CopiedString array in Main.                       *
*********************************************************************************************/
void CopyString(char StringArray[], char CopiedArray[])
{
    for(int i=0; i<SIZE; ++i)
    {
        CopiedArray[i]=StringArray[i];
    }
}


/**************************************MakeUpper*********************************************
*   Action:         Changes characters in the array to an uppercase letter.                 *
*                                                                                           *
*   Parameters:                                                                             *
*       IN:         CopiedArray which has mixed case letters.                               *
*                                                                                           *
*       OUT:        CopiedArray which will have uppercase letters.                          *
*                                                                                           *
*   Returns:        Nothing.                                                                *
*                                                                                           *
*   Precondition:   CopiedArray points to CopiedString array in Main.                       *
*********************************************************************************************/
void MakeUpper(char CopiedArray[])
{
    char c;
    for(int i=0; i<SIZE; ++i)
    {
        c=CopiedArray[i];
        CopiedArray[i]=toupper(c);
    }
}


/***********************************InsertBlanks*********************************************
*   Action:         Moves elements in the array up one spot and replaces the first element  *
*                   with a space as well as the last element.
*                                                                                           *
*   Parameters:                                                                             *
*       IN:         CopiedArray prior to inserting the spaces.                              *
*                                                                                           *
*       OUT:        CopiedArray which will have a space in the first element and at the end.*
*                                                                                           *
*   Returns:        Nothing.                                                                *
*                                                                                           *
*   Precondition:   CopiedArray points to CopiedString array in Main.                       *
*********************************************************************************************/
void InsertBlanks(char CopiedArray[]){
    int temp=0;
    for (int i=SIZE-1; i>0; --i)
    {
        if(CopiedArray[i]=='\0')
        {
            temp=i+1;
        }
        CopiedArray[i]=CopiedArray[i-1];
    }
    CopiedArray[0]=' ';
    CopiedArray[temp]=' ';
    CopiedArray[temp+1]='\0';
}

/********************************ReplacePuncsWithBlanks**************************************
*   Action:         Changes characters which are punctuation to whitepaces.                 *
*                                                                                           *
*   Parameters:                                                                             *
*       IN:         CopiedArray which may include punctuation.                              *
*                                                                                           *
*       OUT:        CopiedArray which will have have replaced punctuations with whitespaces.*
*                                                                                           *
*   Returns:        Nothing.                                                                *
*                                                                                           *
*   Precondition:   CopiedArray points to CopiedString array in Main.                       *
*********************************************************************************************/
void ReplacePuncsWithBlanks(char CopiedArray[])
{
    for (int i=0; i<SIZE; ++i)
    {
        if (ispunct(CopiedArray[i]))
        {
            cout<<CopiedArray[i];
        }
    }

}

void DisplayString(char StringArray[], char CopiedArray[])
{
    for(int i=0; i<SIZE; ++i)
    {
        if(StringArray[i]!='\0')
        {
            cout<<StringArray[i];
        }
        else
        {
            i=(SIZE-1);
        }
    }
    cout<<endl;

    for(int x=0; x<SIZE; ++x)
    {
        if(CopiedArray[x]!='\0')
        {
            cout<<CopiedArray[x];
        }
        else
        {
            x=(SIZE-1);
        }
    }
    cout<<endl;
}

有什么想法吗?

最佳答案

main 函数的开头,StringEnteredCopiedString 都包含垃圾。

GetString 函数从输入中读取一行,并将 StringEntered开头替换为您输入的内容。然后是 \0 字节,其余的垃圾仍然存在。

稍后,CopyString 将您的输入、\0 字节和垃圾复制到 CopiedString

然后,函数 ReplacePuncsWithBlanks 对该内存块的每个字节进行操作。对于每个字节你都调用ispunct,这个函数很特别。它不希望它的参数是 char 类型(您可能认为是这样),而是 unsigned char 类型。查看documentation了解更多详情。

问题是 char 在您的平台上可以有 −128 到 127 之间的值,并且字符串末尾的垃圾肯定包含一些负值。这些值不允许作为 ispunct 函数的参数。

您很幸运,您遇到了断言错误。您的程序也可能无提示地失败或导致您的计算机爆炸。这称为未定义行为

要解决这个问题,您需要将参数转换为正确的类型:

void ReplacePuncsWithBlanks(char CopiedArray[])
{
    for (int i=0; i<SIZE; ++i)
    {
        if (ispunct((unsigned char)CopiedArray[i]))
        {
            cout<<CopiedArray[i];
        }
    }
}

更好的做法是在看到字符串结束字节时立即停止循环:

for (int i = 0; i < SIZE && CopiedArray[i] != '\0'; i++) {

但即便如此,您也需要类型转换。

关于c++ - 使用 ispunct 的函数断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21664630/

相关文章:

c# - 创建内联函数和宏

javascript - 是否可以将 id 名称分配给变量并在 javascript 的函数中使用它

c++ - QDateTime Isodate with timezone ?

C++ 通过参数写入文件或控制台

C初学者: How to copy the arguments from *argv[] to an array of strings using a while loop?

c - 比较2D数组内部,函数内部的字符串

c - 在 C 中增加数组大小的奇怪方法?

MySql 函数

c++ - 可能有多个 while (cin>>input)

c++ - 如何打印 ;使用 CMake 命令的符号?