I'm new to both C++ and StackOverflow and am doing an assignment on creating a Finite State Machine Sequence Detector and I am stuck at a few places. The aim of the program is to accept a binary sequence and a stream of bits with a length greater than this sequence from the user, and search for the sequence in this bit stream using different states for each bit found. I've tried searching for answers on other questions about Run-Time Check Failure #0 but am unable find anything specific to my issue. I still do not understand why these errors are occurring. The specific errors I am facing are in the debugger are
- Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.This takes me to a file called xstring and points to the line Xout_of_range("invalid string position");
- Unhandled exception: std::out_of_range at line 77: if (b.at(i) == s.at(j)) {
- Warning: unreferenced local variable bLength in the second try-catch block.
I know I shouldn't post the entire code but I can't think of ways to shorten it without accidentally leaving out relevant parts.
//C++ program to implement a FSM sequence detector with overlap
#include <iostream>
#include <string>
using namespace std;
struct statePair { //Defining a state pair structure with state binary string and
//output value
string state;
int output=0;
};
int main() {
int sequence, bitStream, count = 0, sLength = 0, bLength = 0,
i = 0 ,j = 0, *indexes, stateNumber = 0;
string s, b, startState, endState, currentState;
statePair *stateList; //Verify
while (true) { /*Taking input of sequence to be detected and
checking if it is valid*/
cout << "Enter a binary sequence to detect: ";
cin >> sequence;
auto s = to_string(sequence);
sLength = s.length();
try {
if (sLength == 0) {
throw sequence;
}
for (int i = 0; i < sLength; i++) {
if ((s.at(i) != '0') && (s.at(i) != '1')) {
throw s;
}
}
}
catch (string s) {
cout << "Error - enter a binary sequence " << endl;
}
catch (int sequence){
cout << "Error - enter a binary sequence " << endl;
}
catch (...) {
cout << "Error - enter a valid binary sequence"<<endl;
}
break;
}
stateList = new statePair[sLength+1]; //Verify
endState = s;
stateList[sLength].state = endState;
stateList[sLength].output = 1;
for (int i = 0; i < sLength; i++) {
stateList[i].state = s.substr(0, stateNumber);
stateList[i].output = 0;
}
while (true) { /*Taking input of bitstream in which the sequence
must be detected and checking if it is valid*/
cout << "Enter a stream of bits: ";
cin >> bitStream;
auto b = to_string(bitStream);
bLength = b.length();
try {
for (int i = 0; i < bLength; i++) {
if ((b.at(i) != '0') && (b.at(i) != '1')) {
throw b;
}
}
if (sLength > bLength) {
throw bLength;
}
}
catch (string b) {
cout << "Error - enter a binary sequence " << endl;
}
catch (int bLength) { //unreferenced local variable bLength
cout << "Error - length of bitstream must be greater than sequence being searched" << endl;
}
catch (...) {
cout << "Error - enter a valid binary sequence" << endl;
break;
}
}
indexes = new int[bLength];
currentState = startState;
for (int i = 0; i < bLength; i++) { //Traversing entire bitstream
if (b.at(i) == s.at(j)) { //Bookmarking index of first bit match
currentState = stateList[j + 1].state;
for (int j = 0; j < sLength; j++) { //Checking for sequence at startIndex
if (i + j < bLength) { //Checking if enough bits are left for sequence to be found
if (b.substr(i, j) == currentState) {
currentState = stateList[j + 1].state;
if (currentState == endState) {
indexes[count] = i;
count++;
j = 0;
i++;
}
else {
currentState = startState;
continue;
}
}
else {
j = 0;
i++;
}
}
else {
break;
}
}
}
}
cout << "The sequence was detected " << count << " times in the given bit stream at the indexes" << indexes << "." << endl;
cin.get(); //To hold the console window to view output
}