退出代码 -1073741510 在 bool if 语句处
Exit code -1073741510 at bool if statement
我正在编写一个程序,提示用户输入.txt文件,然后将ifstream对象和整数数组传递给函数 - count_letters(ifstream &, int *arrayInts(。
然后,该函数从 ifstream 对象中读取每个字符,并存储来自 a-z 的字符频率(不区分大小写(。每个字符都传递给一个 bool 函数,该函数检查它是否为非字母字符。为了可读性,我注释了字母Vect和checkVect向量,因为它们很长。
此时,我的程序退出执行。即使我可以看到返回为"true",也不会执行 for 循环。函数结束前的循环打印出 arrayInt 的内容也永远不会执行。
void count_letters(std::ifstream &fileIn, int *arrayInt)
{
char c; // character variable to read from fileIn
bool cACReturn; // charArrayCheck return value
int aICount = 0; // count for arrayInt in for loop
std::vector<char> alphabetVect {// characters 'a' - 'z'};
while (fileIn.get(ch))
{
tempCh = ch;
cACReturn = charArrayCheck(ch);
std::cout << "cACReturn = " << cACReturn << std::endl;
while (1)
{
// If ch is an alphabetical character
if (cACReturn == true)
{
for (size_t i = 0; i < alphabetVect.size(); (i + 2))
{
if (ch == alphabetVect[i] || ch == alphabetVect[i + 1])
{
std::cout << "success" << std::endl;
arrayInt[aICount]++;
}
aICount++;
}
}
else
{
std::cout << "false" << std::endl;
}
}
}
for (int i = 0; i < 26; i++)
{
std::cout << "In count_letter letterArray[" << i << "] = " << arrayInt[i] << std::endl;
}
}
下面是 charArrayCheck 函数:
bool charArrayCheck(char charIn)
{
std::vector<char> checkVect {// non alphabet characters};
for (size_t i = 0; i < checkVect.size(); i++)
{
if (charIn == checkVect[i])
{
std::cout << "false in charArrayCheck" << std::endl;
return false;
}
else
{
if (i == (checkVect.size() -1))
{
return true;
}
else
{
continue;
}
}
}
}
感谢您的任何和所有帮助。
问题可能是这一行:
for (size_t i = 0; i < alphabetVect.size(); (i + 2))
因为它从不重新分配i
,这是一个无限循环。 aICount
不断递增,最终arrayInt[aICount]++
数组边界之外的访问。这会导致未定义的行为,并且由于导致的所有内存损坏,程序崩溃。
它应该是:
for (size_t i = 0; i < alphabetVect.size(); i += 2)
相关文章:
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 需要有关此 if 语句的帮助
- 转换器练习:跳过 if 语句和 if 语句
- 避免在 C++ 中的 if 语句中递增
- 如果仍然不满足要求,如何使 if 语句重复?
- do while 循环中的 if 语句以 yes 或 no 结尾
- 为什么在此C++代码中触发此 if 语句?
- 如何基于平台在制作文件中制作if语句?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ If 语句,从其他语句重复