I'm supposed to write this c++ that take in 01 and on. for example: 01010101 but not 0,1,10,011,110. Can someone help me figure out what I need to do to fix the problem.

#include <iostream>
#include<stdlib.h>  // for the exit(1) function
using namespace std;

char text[300];
char ToBeChecked;

char lexical(); //identify the characters
void SProd();
void BProd();


int main(){
    cout<<"Enter some strings only 1 and 0 (max. 300 characters"<<endl;
    cin>>text;

    ToBeChecked = lexical(); //identify the character; find the first letter and give it to ToBeChecked
    SProd();

    if(ToBeChecked == '\0')
        cout<<"Valid"<<endl;
        else
        cout<<"Invalid"<<endl;
    cin.get();
    return 0;
}

char lexical(){
    static int index = -1;   //a memory box named index with a value of -1; is static so it won't change.
                            //is -1 because -1 to 1 is 0; everything move on to next one
    index++; //update index
    return text[index]; //return the value of index
}

void SProd(){

	 if(ToBeChecked != '0' ) {
		 cout<<"Invalid"<<endl;
		 	 exit(1);
	  }
	 else{
		  	  BProd();
		 	   ToBeChecked = lexical();
	  }


	/*if(ToBeChecked != '0')
			{
			    BProd();
				ToBeChecked =lexical();
			}
			else
			{
			if(ToBeChecked == '1')
			{
					ToBeChecked = lexical();
					SProd();
			}
				else
				{
					SProd();
					}
			}*/
}

void BProd(){
	/*if(ToBeChecked == '1'){
		SProd();
		ToBeChecked = lexical();

	}
	else{*/
	   if(ToBeChecked != '1')
	   {
		   cout<<"Invalid"<<endl;
			 exit(1);
	   }
	   else
		   SProd();
		   ToBeChecked = lexical();


	}

		/*if(ToBeChecked == '1')
		{
			SProd();
			ToBeChecked = lexical();

		}
			else
			{
				cout<<"Invalid"<<endl;
					 exit(1);
				}*/

What happens when you do:

char text[20];
cin >> text;

??

I really don't know, but I think it will allow someone to enter too many characters and 'sploit the program.

const std::streamsize BUFFER_MAX = 1024;
	char buffer[BUFFER_MAX] = {0};
	std::cin.getline(buffer,BUFFER_MAX,'\n');

you can do this easily with a state machine.

search on google how to code a simple state machine.

What happens when you do:

char text[20];
cin >> text;

??

I really don't know, but I think it will allow someone to enter too many characters and 'sploit the program.

const std::streamsize BUFFER_MAX = 1024;
	char buffer[BUFFER_MAX] = {0};
	std::cin.getline(buffer,BUFFER_MAX,'\n');

What I was trying to do is that when some enter 1 than it prints invalid. if they enter 0 it prints invalid, if the enter 10 it prints invalid, if they enter 01 it prints valid, if the enter 0101 it prints valid. So 0 always have to come first and always follow by 1. another example: 0101010101 prints valid

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

class BitStream  {
   std::string Stream;
   typedef std::string::const_iterator I;

   bool Compare (I First,I Second)  {
      bool first=0,second=0;           
      std::istringstream i( std::string (1,*First) ); //construct a string out of a char
      i >> first;  //convert the string to bool

      std::istringstream o( std::string (1,*Second) );
      o >> second;
      
      return (first) < (second);  // compare value of iterators
    }

public:
   BitStream () {}   
   BitStream (std::string stream_) : Stream(stream_)  {} 
   ~BitStream () {}

   bool IsValid ()  {
      I i=Stream.begin();
      ++i;                  //get step one ahead 
      bool isValid=0;
      for (;i!=Stream.end();++i)  {
         isValid=Compare(i-1,i);   //compare second with first
         if (isValid)  {
            break;
          }
       }
      return isValid;
    }
 };

int main ()  {
   BitStream b("11110111111");
   std::cout << b.IsValid();
 }
This article has been dead for over six months. Start a new discussion instead.