Thread: spell checker using finite state machine

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Question spell checker using finite state machine

    I need someone to view and debug the lines of code below. Am trying to spell check the word "and" using state to state transition, but I can't get the code to run.

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    string in_str;
    int n;
    void spell_check()
    {
         int i;
         FILE *in_file;
    
         while (!EOF(in_file))
         {
               fscanf(in_str);
               n = strlen(in_str);
               start(in_str,n);
         }
    }
    
    void start()
    {
         char next_char;
    
         int i = 0;
         if (n == 0)
         {
               cout<<"This is an empty string";
               exit();//do something here to terminate the program
         }
         else{
              next_char = in_str[i];
    
              if(next_char == 'a')
              {
                           i++;
                           if(i >= n) error();
                           else state_A(i);
              }
              else error();
         }
    }
    
    void state_A(int i)
    {
         if(in_str[i] == 'n')
         {
                      i++;
                      if(i<n) state_AN(i);
                      else error();
         }
         else error();
    }
    
    void state_AN(int i)
    {
         if(in_str[i] == 'd')
         {
                      if(i == n-1)
                           cout<<" Your keyword spelling is correct";
                      else
                          cout<<"Wrong keyword spelling";
         }
    }
    
    int main()
    {
        spell_check();
        return 0;
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You can just randomly cobble together tokens. The tokens must be syntactically and semantically valid.

    Why you start correcting the code? Or at least tell us why you can't get it to run.

    Soma

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Thanks, am just learning c++ and my I was asked to spell check any word using a finite state machine. Am just hoping I could make use of the code if I can debug it but I ran out of ideas. it's urgent. Durell

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
         FILE *in_file;
    This is C routine for file IO.
    You should use iostream

    Code:
         while (!EOF(in_file))
    How you came out with this line of code?
    Take a sample for reading file in C++ (working sample)
    and start with it, before you continue any father... No point to write a 100-lines program if you cannot make even 3-lines program work
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I'm pretty sure phantomotap meant to say you can't just type stuff and expect it to compile and/or run, opposed to saying you "can".

    Quote Originally Posted by DurellP
    it's urgent.
    See Don't flag your question as “Urgent”, even if it is for you for an explanation of why not to say this.

    In your code you're mixing C and C++, which is certainly not a good idea. Pick one and stick with it. It seems the "majority" is C++, so get rid of the C stuff and use only C++. For example, you're using "FILE*", "scanf", "strlen", etc., which are all C-oriented functions.

    Also, you declare a "FILE*" then immediately work with it, which isn't any use. You'd have to open it and make sure it was successfully opened before doing anything with it. Read this link, or others, to get familiar with C++ file objects: Input/Output with files. Similarly you are calling "fscanf" incorrectly. But after learning about C++ files, you should be able to understand how to do it correctly, and in C++ since this is a C++ program.

    Also, is "error()" a function? I've never used it before, so either it isn't, or it's something specific to your compiler. If you get compiler errors about this function, then either implement it yourself or do something instead of calling "error()".

    After you fix all this, post your complete updated code, along with all compiler error/warning messages and questions.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'm pretty sure phantomotap meant to say you can't just type stuff and expect it to compile and/or run, opposed to saying you "can".
    ^_^;

    But wouldn't it be nice if you could?

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Will my memory leak? --school project
    By steals10304 in forum C++ Programming
    Replies: 10
    Last Post: 02-24-2010, 03:04 PM
  2. Finite State Machine
    By ArlexBee-871RBO in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2008, 11:59 AM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. Finite State Machine Project Help
    By ryanbradley in forum C++ Programming
    Replies: 4
    Last Post: 03-06-2004, 10:23 AM
  5. Designing State Machine
    By axon in forum Tech Board
    Replies: 3
    Last Post: 11-06-2003, 12:13 PM

Tags for this Thread