c++ - 我的代码适用于输入文件,但不适用于其他文件。(调试断言错误)
问题描述
编写并测试一个程序,提示用户输入文件名和要测试的字符串。在文件中搜索指定字符串的每次出现——找到字符串后,显示包含它的行。找到所有出现的字符串后,将显示该字符串在文件中出现的次数。提示:你可以使用字符串成员函数 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 出现错误
解决方案
我调试了程序,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 提议的那样投射它,它就会起作用。
您还可以修复输入文件。
一个非常有趣和微妙的错误。. .
推荐阅读
- scala - 如何使用 Scala 将 CSV 中的值分配给单个变量
- c# - 如果它为零,则比较十进制忽略精度
- ios - UICollectionView 未从 SDWebImage 加载图像
- python - 抓取后选择部分html
- cookies - 以新方式扩展在后台页面和 Safari 之间共享 cookie
- ios - 迅速。如何将文本行附加到 file.txt 的顶部?
- python - 遍历所有像素以检查哪些像素是白色的,哪些是黑色的
- perl - perl grep 匹配来自哈希表的变量
- python - 在 Tensorflow Graph 的输入层之前添加 base64 解码层
- android - 如何在 C# 中以编程方式列出来自设备(iOS/Android)的 SMS 对话?