Finite State Machine Project Help

This is a discussion on Finite State Machine Project Help within the C++ Programming forums, part of the General Programming Boards category; I am currently taking a class in C++ which has been going very well until our current assignment. The asignment ...

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

    Finite State Machine Project Help

    I am currently taking a class in C++ which has been going very well until our current assignment. The asignment is to build a finite state machine that will take in inputs from the user see whether it is an identifier or a digit and the transistion to an approriate stage to deal with those. For example someone inputs: R2D2,Time,48,2,rate,555666. would output

    R2D2 -identifier
    Time -identfier
    48 -number
    2 -number
    and so on until it reaches the end. The program is supposed to parse each character to see what it is and from there transition to a different state. If it starts as a digit it goes from start state to digit state where it checks for more digits until it reads a comma which then says its done and it s a number so it transitions to the number state which saves it as a number and then transistions back to start. If it is a character then it goes to the build identfier state which looks for a letter or didgit to make up the name and the once again a , will terminate it and call it to the identifier state which then goes back to start. This parsing repeats until a period is found which ends the program.

    A little more background information is that why are in a chapter dealing with functions so I am assuming a vast majority of this will be functions. I was informed by the professor using isalpha and isdigit will be crucial but he gave no information on there implementation and I can't find a source that explains them in a way I understand, he also said to use a case switchblock. So my idea was once I get the isalpha is digit figured out I could put those in a case switch block and when it finds if its a char or digit it goes to the approaiate state. As far as the states are concerned I was going to make each one a function and when it tansitioned just have it call the next function.

    Sorry for the super long post buyt I am completly lost and can use any help anybody can offer. Thanks for taking the time to read this and any input is greatly appreciated.

    Ryan Bradley

  2. #2
    Registered User
    Join Date
    Jan 2004
    Posts
    33
    If your using strings, you could iterate through the string and us isalpha, isalnum, isdigit etc. If your using char or char*, try using pointer arithmetic to move along the string, and parse the input. Since the commas specify the begining of a new type in your input (identifier, number etc) when you reach one of them via pointer or iterator ignore it and make sure the program knows that a new word must be recognized. Add the results either into an array or vector, and then output them at the end of the program.
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    2
    The only problem is we have not yet covered vectors, arrays or pointers and as such am unable to use them. I am basically able to work with simple user created functions, loops and other very basic C++ features.
    Thanks,
    Ryan Bradley

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Read this

    You need to draw a state transition diagram, with I guess these states
    - initial (you start here)
    - identifier
    - number
    - comma
    - terminal (you finish here)

    In each instance, you
    - look at your current state
    - look at the next character
    to determine what your new state should be.

    When you change state, you output the sequence of symbols you accumulated in the old state.

    > I was informed by the professor using isalpha and isdigit will be crucial but he gave no information on there implementation
    They're prototyped in ctype.h (or just ctype for C++ users)
    You use them like
    if ( isalpha('a') )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    33
    Code:
    #include <iostream>
    
    int main()
    {
    
    	char input[20]; //the buffer should be larger
    	std::cout << "Enter input: ";
    	std::cin >> input;
    
    	int count = 0;
    
    	for(int i = 0; i < strlen(input); i++) //iterate through the string
    	{
    		if(isalpha(input[i]))
    			std::cout << "alpha\n"; 
    		if(isdigit(input[i]))
    			std::cout << "digit\n";
    	}
    
    
    	return 0;
    }
    Of course in the if statements you would check for the stuff that Salem just mentioned.
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. State machine
    By sergio in forum General Discussions
    Replies: 3
    Last Post: 07-03-2009, 09:03 AM
  2. State Machine Controller
    By scott_ill in forum C# Programming
    Replies: 0
    Last Post: 06-25-2009, 02:57 AM
  3. input/switch statement issues
    By peanut in forum C Programming
    Replies: 5
    Last Post: 10-27-2006, 02:58 PM
  4. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 08:01 AM
  5. Function pointer question
    By sbayeta in forum C Programming
    Replies: 9
    Last Post: 08-06-2004, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21