Finding an Incremental Consecutive Hex Pattern

I am trying to make a state machine that recognizes 2 patterns: 4 consecutive inputs that are the same (1111 or FFFF) and 4 consecutive inputs that are incremental (1234 or 26AE). I have figured out a solution (possibly not the most concise) for the 4 equal consecutive inputs. But I am stuck on how to check that the 4 numbers are incremental.

I know my second if statement is incorrect and it is just a place holder for when I include the array. Is it possible to have an array that contains letters and numbers and can I use it to check if there is a pattern of +1, +2, +3, or +4 etc...?

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <memory.h>

using namespace std;


int main(){

    int samestate;
    int incrstate;
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int temp;
    bool test = true;
    
    char hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

  while(test == true){
    cout << "Enter a number:";
    cin >> temp;

    a = b;
    b = c;
    c = d;
    d = temp;



    if(a==b && b==c && c==d){
        samestate = 1;

    }else{
        samestate = 0;
    }
    
    if(b==a+1 && c==a+2 && d==a+3){
        incrstate = 1;

    }else{
        incrstate = 0;
    }

cout << "Pattern:" << a << b << c << d << endl;
cout<< "Same state:" << samestate << endl;
cout<< "Increment state:" << incrstate << endl;
 }

}
Last edited on
This is not a state machine. But first of all your input is wrong. Since it is supposed to be hex it cannot be int. It is certainly char.

A state machine is usually realized with enum and switch. Lets assume that the user enters one hex character after another and the requirement is that whenever 4 equal hex numbers are entered there is some output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
enum state_enum { startup, comparable, ... };
...
state_enum state = startup;
char temp;
...
cin >> temp;
switch(state)
{
case startup: // Since at startup there is nothing to compare -> just change the state
  state = comparable;
break;

case comparable: // Now it is actually comparable
  if(last == temp) // if there is equality the state is changed
    state = in_compare;
  if(state != in_compare) // Note: when 'in_compare' this case goes thru... i.e. no break!
    break;

case in_compare:
  if(last == temp)
  {
    ++compare_success_count; // It is necessary to count the number of successful comparisons
    if(compare_success_count > ...)
      state = compare_success;
  }
  else // If not equal we need to fall back to the comparable state...
    state = comparable;
  if(state != compare_success) // Note: compare_success goes thru again
    break;

case compare_success:
  cout << "Success";
  .... // The next state -> comparable?
break;
  ...
}
last = temp;
...
Note that it is not tested and you need to put in more effort to make it actually run.

Also note that there might be an error state required when the user enters an invalid character.
Topic archived. No new replies allowed.