While with switch statement

Hi, I'm trying to complete the below code and would appreciate any help you all can offer. I got some help with it and am still stuck at the "TO DO" sections. Please assist if you can. Thank you!.

#include "stdafx.h"
#include
#include
//#include "memory.h"
//#include "stdio.h"

using namespace System;
using namespace std;

// Function Prototype
bool stateMachine(char *pInputBuffer, char *pOutputBuffer);

//**************************************************************
// Function: main()
//**************************************************************
int main()
{
// prompt user to enter data
cout << "Enter a string with '111' being the start & end delimiter,
(e.g. 1110110111):

";

// Capture the user input
char InputBuffer[256];
memset (InputBuffer, 0, sizeof(char) * 256);
cin >> InputBuffer;

// process the user input
bool OutputFlag = false;
char OutputBuffer[256];
memset (InputBuffer, 0, sizeof(char) * 256);
OutputFlag = stateMachine(InputBuffer, OutputBuffer);

// Display the user output
if (OutputFlag == true)
{
// TO DO: Display the OutputBuffer; Need help here also!!!
????????
}

return 0;
}
//**************************************************************
// Function: stateMachine()
//**************************************************************
bool stateMachine(char *pInputBuffer, char *pOutputBuffer)
{
char *pBuf = pInputBuffer;
char *pOut = pOutputBuffer;
// test the user input
int length = 0;
length = strlen(pInputBuffer);
if (length <= 6){
return false;
}

// process the user input
int state = 0;
bool outputFlag = false;
while (true)
{
switch (state)
{
case 0:
{
if (*pBuf != '1')
{ // Did not find a 1
state = 0;
}
else
{
// Found a 1
state = 1;
}
break;
}
case 1:
{
if (*pBuf != '1')
{ // Did not find a 1
state = 0;
}
else
{
// Found the second 1
state = 2;
}
break;
}
case 2:
{
if (*pBuf != '1')
{ // Did not find a 1
state = 0;
}
else
{
// Found the third 1
state = 3;
}
break;
}
case 3:
{
// Copy the content of the string to the ouput buffer until
// a 1 is found.
for (*pBuf = 1; *pBuf = '111'; *pBuff++)
???????
//To DO: Need help here!!!.
break;
}

default:
{
cout << "Your input consists of invalid strings. ";
}
}// End of switch
} // End of while loop

}

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories