首页 > 解决方案 > 我的代码适用于输入文件,但不适用于其他文件。(调试断言错误)

问题描述

编写并测试一个程序,提示用户输入文件名和要测试的字符串。在文件中搜索指定字符串的每次出现——找到字符串后,显示包含它的行。找到所有出现的字符串后,将显示该字符串在文件中出现的次数。提示:你可以使用字符串成员函数 find()。

这是我的代码,正如我所说,它适用于一些句子但不是全部,它似乎与长度没有任何关系,就像我重复我知道有效的一行一样,它不会遇到任何错误,可以有人解释吗?

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cctype>

using namespace std;

void find_common_words(string, int);
vector<string> words;

int main(){ 
    vector<string> line;
    string input;
    char filename[50];
    ifstream inputFile;
    cout << "Enter File Name:" << endl;
    cin.getline(filename, 50);
    inputFile.open(filename);
    //TESTING IF FILE IS OPEN
    if (!inputFile.is_open()){
        cout << "File Wasn't Opened!" << endl;
        return 0;
    }

    while (getline(inputFile, input)){
        line.push_back(input);
    }
    int size_of_array = static_cast<int>(line.size());
    
    for (int i = 0; i < size_of_array; i++){
        istringstream iss(line[i]);
        int word_number = 0;

        do {
            string word;
            iss >> word;
            words.push_back(word);
            word_number++;
        } while (iss);
    }
    string commonword;
    cout << "Enter a word to search for (without punctuation)" << endl;
    cin >> commonword;
    int size_of_array2 = static_cast<int>(words.size());
    find_common_words(commonword, size_of_array2);

    return 0;
}

void find_common_words(string search, int sizeofarray2) {   
    //CONVERSION TO STRING WITHOUT PUNCTUATION
    for (int i = 0; i < sizeofarray2; i++){
        string temp_string = words[i];
        for (int j = 0, len = temp_string.size(); j < len; j++){
            if (ispunct(temp_string[j])){
                temp_string.erase(j--, 1);
                len = temp_string.size();
            }
        }
        words[i] = temp_string;
    }
    //SEARCHING FOR SAME WORDS
    int line_number = 1;
    int words_found = 0;
    for (int i = 0; i < sizeofarray2; i++){
        if (search == words[i]){
            cout << search << " Was found on line " << line_number << endl;
            words_found++;
        }
        if (words[i].length() == 0){
            line_number++;
        }
    }
    cout << line_number - 1 << " lines checked, " << words_found << " matches " << endl;
}

这是我的输入文件(随机生成):

It's a very big deal.
Carl won the spelling bee and got a trophy!
Why is your cat so big?
Do you have a big bowl I can borrow?
It's a big company.
Penguins live in the Antarctica.
Don’t be silly, you're going to the game!
What are you talking about?
He threw up in the trash can!
You're so ratchet!
But she is a good caretaker.
Tom is looking for a bigger house to live in.

调试断言表达式 c> = -1 && c < = 255 出现错误

标签: c++stringvector

解决方案


我调试了程序,WhozCraig 是对的。在您输入的文本中

Don’t be silly, you're going to the game!

在“Don't”这个词中,你没有像“It's”或“you're”这个词中的正常撇号

这个特殊字符在 temp_string 中编码为 -110。在std::ispunct的描述中,您可以阅读:

如果 ch 的值不能表示为 unsigned char 且不等于 EOF,则行为未定义。

-110 不适合并导致调试断言错误。如果你unsigned char像 WhozCraig 提议的那样投射它,它就会起作用。

您还可以修复输入文件。

一个非常有趣和微妙的错误。. .


推荐阅读