1

I was assigned to create an array check (to see if the array is increasing, decreasing, or neither [then exiting if neither]) and a recursive binary search for one of my assignments. I was able to do these things after some help from my peers, but I need help in finding what seems to be causing the error

terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid Aborted

when running the code. I Googled this error and this error seems to be vague or I just am not understanding. It compiles without errors, but I need help in what finding what I did wrong. It is able to run without the binarySearchR function and its associating code, as the array check on its own was the previous assignment. Below is the code, and I thank you so much in advance!

#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;
        }
    }
}
cyankai
  • 11
  • 1
  • This error typically indicates memory corruption/undefined behavior. The cause could be anything. Use a debugger to figure out where the exception is being thrown. There's at least one bug in your code: "while(!(fileIn.eof()))" [is always a bug](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Sam Varshavchik Mar 01 '16 at 03:04
  • Additionally: you will execute a binary search when the array is either in ascending or descending order, but your binary search only works when the array is in ascending order. Also, your second recursive call in the binary search is wrong. Multiple problems with this code. – Sam Varshavchik Mar 01 '16 at 03:05

1 Answers1

0

The easy way: add a bunch of cout s to see where you program goes and what the values are.

Pros

  • Easy to do

Cons

  • Requires a recompile each time you want to add more info

The hard way: Learn to use a debugger

Pros

  • Can inspect "on the fly"
  • Don't need to rebuild
  • Can use what you learn in every other C++ program

Cons

  • Requires a bit of research to learn how to do it.
John3136
  • 28,809
  • 4
  • 51
  • 69