Question

Can someone help with this C++ code. I am trying to compile and I keep running...

Can someone help with this C++ code. I am trying to compile and I keep running into these 4 errors. 

#include <iostream>
#include <cassert>
#include <string>
using namespace std;
typedef int     fsm_state;
typedef char    fsm_input;
bool is_final_state(fsm_state state)
{
  return (state == 3) ? true : false;
}
fsm_state get_start_state(void)
{
  return 0;
}
fsm_state move(fsm_state state, fsm_input input)
{
  // our alphabet includes only 'a' and 'b'
  if (input != 'a' && input != 'b')
    assert(0);
  switch (state)
  {
    case 0:
      if (input == 'a')
      {
        return 1;
      }
      else if (input == 'b')
      {
        return 0;
      }
      break;
    case 1:
      if (input == 'a')
      {
        return 1;
      }
      else if (input == 'b')
      {
        return 2;
      }
      break;
    case 2:
      if (input == 'a')
      {
        return 1;
      }
      else if (input == 'b')
      {
        return 3;
      }
      break;
    case 3:
      if (input == 'a')
      {
        return 1;
      }
      else if (input == 'b')
      {
        return 0;
      }
      break;
      default:
        assert(0);
  }
}
bool recognize(string str)
{
  if (str == "")
    return false;
  fsm_state state = get_start_state();
  string::const_iterator i = str.begin();
  fsm_input input = *i;
  while (i != str.end())
  {
    state = move(state, *i);
    ++i;
  }
  if (is_final_state(state))
    return true;
  else
    return false;
}
// simple driver for testing
int main(int argc, char** argv)
{
  recognize(argv[1]) ? cout < 1 : cout < 0;
  return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, I have fixed all compile time errors.

Please test with your test cases and let me know in case of any issue.

#include <iostream>
#include <cassert>
#include <string>
using namespace std;
typedef int fsm_state;
typedef char fsm_input;
bool is_final_state(fsm_state state)
{
return (state == 3) ? true : false;
}
fsm_state get_start_state(void)
{
return 0;
}
fsm_state move(fsm_state state, fsm_input input)
{
// our alphabet includes only 'a' and 'b'
if (input != 'a' && input != 'b')
assert(0);
switch (state)
{
case 0:
if (input == 'a')
{
return 1;
}
else if (input == 'b')
{
return 0;
}
break;
case 1:
if (input == 'a')
{
return 1;
}
else if (input == 'b')
{
return 2;
}
break;
case 2:
if (input == 'a')
{
return 1;
}
else if (input == 'b')
{
return 3;
}
break;
case 3:
if (input == 'a')
{
return 1;
}
else if (input == 'b')
{
return 0;
}
break;
default:
assert(0);
}
return -1; // Error code
}
bool recognize(string str)
{
if (str == "")
return false;
fsm_state state = get_start_state();
string::const_iterator i = str.begin();
fsm_input input = *i;
while (i != str.end())
{
state = move(state, *i);
++i;
}
if (is_final_state(state))
return true;
else
return false;
}
// simple driver for testing
int main(int argc, char** argv)
{
recognize(argv[1]) ? cout << "1" : cout << "0";
cout<<endl;
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Can someone help with this C++ code. I am trying to compile and I keep running...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • Hi, I need to change the conversion where stack is into a class stack and add...

    Hi, I need to change the conversion where stack is into a class stack and add a template <class type> PROMPT: write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. if the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. #include #include #include #include #include using namespace std; double conversion(string); int...

  • I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not...

    I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not declared in this scope" in my main.cpp. Here are my codes. main.cpp #include <iostream> #include <string> #include "functions.h" using namespace std; //definition of the main function. //takes arguments from the command-line. int main(int argc, char *argv[]) { //Determine if you have enough arguments. //If not, output a usage message and exit program if (argc<2 || (argc == 2 && argv[1][0] == '-')) { //call...

  • I am trying to write a c++ program that will add two binary numbers that are...

    I am trying to write a c++ program that will add two binary numbers that are entered into the command line. I cannot get the program to send anything to stdout. code attached. int main(int argc, char *argv[]) //           IN       IN {    char partialSum[MAX_DIGITS + 1]; //partial sum of the binary numbers    char sum[MAX_DIGITS + 1];       //sum of the binary numbers    bool noError;       //no error flag       //Exit if insufficient arguments...

  • I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ________...

    I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...

  • Can someone help me fix my C code. I am getting segmentation fault along with missing...

    Can someone help me fix my C code. I am getting segmentation fault along with missing termination " /* These are the included libraries. */ #include #include #include #include void printTriangle(char *str); int main() { char sentence[81]; int done = 0; while(done != 1) { printf("Please enter the sentence or quit: "); fgets(sentence, 80, stdin); if(strcmp(sentence, "quit") == 0) done = 1; else printTriangle(sentence); } } void printTriangle(char *str) { int index = 0; int line = 1; int i;...

  • I am trying to run this program in Visual Studio 2017. I keep getting this build...

    I am trying to run this program in Visual Studio 2017. I keep getting this build error: error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". 1>Done building project "ConsoleApplication2.vcxproj" -- FAILED. #include <iostream> #include<cstdlib> #include<fstream> #include<string> using namespace std; void showChoices() { cout << "\nMAIN MENU" << endl; cout << "1: Addition...

  • Can someone please edit my current code to accept a user input for the text file...

    Can someone please edit my current code to accept a user input for the text file name in lieu of how it currently will read data1.txt. Also if the user enters a data file that isn't ready to be used, cout a statement "cout << "Could not open file " << userInputforData.txt << endl; Another condition that I need to add is if there is a special character anywhere in the word it cannot be an acceptable variable name. could...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT