C++ 我如何追溯错误
我被指派创建一个数组检查(查看数组是否在增加、减少或两者都没有[如果两者都没有则退出]),并对我的一个赋值进行递归二进制搜索。在我的同龄人的一些帮助下,我能够做这些事情,但是我需要帮助来找到导致错误的原因 在抛出“std::logic_error”实例后调用terminate what():基本\u字符串::\u S\u构造null无效 流产 当运行代码时。我在谷歌上搜索了这个错误,这个错误看起来很模糊,或者我就是不理解。它编译时没有错误,但我需要帮助找出我做错了什么。它可以在没有BinarySearch函数及其关联代码的情况下运行,因为数组检查本身就是上一个赋值。下面是代码,我提前非常感谢您C++ 我如何追溯错误,c++,debugging,logic,stdstring,terminate,C++,Debugging,Logic,Stdstring,Terminate,我被指派创建一个数组检查(查看数组是否在增加、减少或两者都没有[如果两者都没有则退出]),并对我的一个赋值进行递归二进制搜索。在我的同龄人的一些帮助下,我能够做这些事情,但是我需要帮助来找到导致错误的原因 在抛出“std::logic_error”实例后调用terminate what():基本\u字符串::\u S\u构造null无效 流产 当运行代码时。我在谷歌上搜索了这个错误,这个错误看起来很模糊,或者我就是不理解。它编译时没有错误,但我需要帮助找出我做错了什么。它可以在没有BinaryS
#include <iosteam>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
int checkArraySort (string *fileLines, int numberOfLines);
int binarySearchR (string *fileLines, string searchKey, int iMin, int iMax);
int main ()
{
int numberOfLines = 0;
string searchKey = 0;
cout << "Input search key: ";
cin >> searchKey;
ifstream fileIn;
fileIn.open("words_in.txt");
string line;
if (fileIn.eof()) /* Checks file to see if it is blank before proceeding */
{
exit (EXIT_SUCCESS);
}
else
{
while(!(fileIn.eof()))
{
fileIn >> line;
numberOfLines++;
}
fileIn.close(); /* closes fileIn, need to reopen to reset the line location */
fileIn.open("words_in.txt");
string *fileInLines;
fileInLines = new string[numberOfLines];
for (int i = 0; i < numberOfLines; i++)
{
fileIn >> line;
fileInLines[i] = line;
}
fileIn.close(); /* closes fileIn */
int resultingCheck = checkArraySort(fileInLines, numberOfLines);
if (resultingCheck == -1)
{
cout << "The array is sorted in descending order." << endl;
}
else if (resultingCheck == 1)
{
cout << "The array is sorted in ascending order." << endl;
}
else
{
cerr << "ERROR: Array not sorted!" << endl;
exit (EXIT_FAILURE);
}
int searchResult = binarySearchR (fileInLines, searchKey, 0, numberOfLines);
if (!searchResult == -1)
{
cout << "Key found at index " << searchResult << "." << endl;
}
else
{
cout << "Key not found at any index." << endl;
}
exit (EXIT_SUCCESS);
}
}
int checkArraySort (string *fileLines, int numberOfLines)
{
int result = 1; /* Ascending by default */
for (int i = 1; i < numberOfLines; i++) /* Checks if decending */
{
if (fileLines[i] < fileLines[i-1])
{
result = -1;
}
}
if (result == -1) /* Makes sure it is descending (or if it is neither) */
{
for (int i = 1; i < numberOfLines; i++)
{
if (fileLines[i] > fileLines[i-1])
{
result = 0;
}
}
}
return result;
}
int binarySearchR (string *fileLines, string searchKey, int iMin, int iMax)
{
// so, its gotta look at the center value and each times, it discards half of the remaining list.
if (iMax < iMin) /* If the minimum is greater than the maximum */
{
return -1;
}
else
{
int iMid = (iMin + iMax) / 2;
if (fileLines[iMid] > searchKey) /* If the key is in the lower subset */
{
return binarySearchR (fileLines, searchKey, iMin, iMid - 1);
}
else if (fileLines[iMid] < searchKey) /*If the key is in the upper subset */
{
return binarySearchR (fileLines, searchKey, iMin, iMid + 1);
}
else /*If anything else besides the two */
{
return iMid;
}
}
}
#包括
#包括
#包括
#包括
使用名称空间std;
int checkArraySort(字符串*文件行,int numberOfLines);
int-binarysearch(字符串*文件行、字符串搜索键、int-iMin、int-iMax);
int main()
{
int numberOfLines=0;
字符串searchKey=0;
cout>搜索键;
ifstreamfilein;
fileIn.open(“words_in.txt”);
弦线;
if(fileIn.eof())/*在继续之前检查文件是否为空*/
{
退出(退出成功);
}
其他的
{
而(!(fileIn.eof())
{
fileIn>>行;
numberOfLines++;
}
fileIn.close();/*关闭fileIn,需要重新打开以重置行位置*/
fileIn.open(“words_in.txt”);
字符串*fileinline;
fileInLines=新字符串[numberOfLines];
对于(int i=0;i>行;
fileInLines[i]=行;
}
fileIn.close();/*关闭fileIn*/
int resultingCheck=checkArraySort(fileInLines,numberOfLines);
如果(结果检查==-1)
{
cout简单方法:添加一组cout
以查看程序的运行位置和值
专业人士
- 容易做
缺点
- 每次要添加更多信息时都需要重新编译
艰难之路:学习使用调试器
专业人士
- 可以“在飞行中”检查
- 不需要重建
可以使用你在其他C++程序中所学的内容
缺点
- 需要一些研究来学习如何做
简单方法:添加一堆cout
以查看程序的运行位置和值
专业人士
- 容易做
缺点
- 每次要添加更多信息时都需要重新编译
艰难之路:学习使用调试器
专业人士
- 可以“在飞行中”检查
- 不需要重建
可以使用你在其他C++程序中所学的内容
缺点
- 需要一些研究来学习如何做
此错误通常表示内存损坏/未定义的行为。原因可能是任何原因。请使用调试器找出引发异常的位置。代码中至少有一个错误:“while(!(fileIn.eof())”。此外:当数组按升序或降序排列时,您将执行二进制搜索,但您的二进制搜索仅在数组按升序排列时有效。此外,二进制搜索中的第二个递归调用错误。此代码存在多个问题。此错误通常表示内存损坏/未定义的行为原因可能是任何原因。请使用调试器找出引发异常的位置。代码中至少有一个错误:“while(!(fileIn.eof())”。此外:当数组按升序或降序排列时,您将执行二进制搜索,但二进制搜索仅在数组按升序排列时有效。此外,二进制搜索中的第二个递归调用错误。此代码存在多个问题。