The flowchart is on the attachment. Please check it out

I also get this error:

error: cannot convert `__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >' to `char' in assignment|

/***************************************************
  The following program will take user's input
  and check whether it is in the following language:

    <S> --->  a <B> | <A> | b
    <A> --->  c <A> |  c
    <B> --->  d | <A>
****************************************************/


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


char* SProd(char* ThisOne ); // <S> --> a <B>  | <A>  | b
char* AProd(char* ThisOne ); // <A> --> c <A>  | c
char* BProd(char* ThisOne ); // <B> --> d      | <A>
char* XProd(char* ThisOne, char x);

int main()
{


	string text;
	char *ThisOne;

	 cout<<"Enter a string"<<endl;
	 cin>>text;


    *ThisOne = text.begin();
	 ThisOne = SProd(ThisOne);


	if(*ThisOne== '\0')  //Check whether there is an extra character at the end
		cout<<"valid"<<endl;
	else
		cout<<"Invalid"<<endl;
	system("pause");
   	return 0;
}



char* XProd(char* ThisOne, char x)
{
		ThisOne = XProd(ThisOne, 'a');
		ThisOne	 = XProd(ThisOne, 'b');
		ThisOne   = XProd(ThisOne, 'c');
		ThisOne	 = XProd(ThisOne,  'd');
		return ThisOne;
}

 // <S> --> a <B>  | <A>  | b
char* SProd(char* ThisOne )
{
	if (*ThisOne == 'b')
	{
		ThisOne++;
		return ThisOne;
	}

	else {
		if (*ThisOne == 'a')
	{
		ThisOne++;
		 ThisOne = BProd(ThisOne);
		 return ThisOne;
	}
		else
		{
			 ThisOne = AProd(ThisOne);
			 return ThisOne;
		}
	}
}




// <A> --> c <A>  | c
char* AProd(char* ThisOne)
{
	if(*ThisOne != 'c')
	{
		cout<<"Invalid"<<endl;
		exit(1);
	}
	else
	{
		ThisOne++;
		while (*ThisOne == 'c')
			ThisOne++;
			return ThisOne;
	}



}


// <B> --> d      | <A>
char* BProd(char* ThisOne)
{
	if(*ThisOne = 'd')
	{
		ThisOne++;
		return ThisOne;
	}
	else
	{
        AProd(ThisOne);
	}

}

The problem seems to be coming from the fact that you're using pointers.

I get screwed with them all the time. >.<

And I compiled it and got the exact same error as well....

Edited 5 Years Ago by spoonlicker: n/a

Iterator is not a pointer and you cannot automatically cast it in line 33 (also you try to convert it to char, I think you intended to write ThisOne = text.begin();, which isn't correct). If you want to use char* I would suggest using array of chars instead of string (C way) or change your functions to use string::iterator (STL way).

Or you can use dirty trick, which is not guaranteed to work (but should). String is usually implemented like vector, using continuous memory for storing elements, bytes (I know that vector must have this property). So if you take address of first element of the string you should get pointer to beginning of char array, but it doesn't have to be null terminated:

#include <iostream>
#include <string>
int main(){
	std::string s="abba";
	char* cp=&(s[0]);
	for(int i=0; i<4; i++) std::cout<<*cp++;
}

This is the quickest way to repair this code, although I wouldn't recommend this style.

Iterator is not a pointer and you cannot automatically cast it in line 33 (also you try to convert it to char, I think you intended to write ThisOne = text.begin();, which isn't correct). If you want to use char* I would suggest using array of chars instead of string (C way) or change your functions to use string::iterator (STL way).

Or you can use dirty trick, which is not guaranteed to work (but should). String is usually implemented like vector, using continuous memory for storing elements, bytes (I know that vector must have this property). So if you take address of first element of the string you should get pointer to beginning of char array, but it doesn't have to be null terminated:

#include <iostream>
#include <string>
int main(){
	std::string s="abba";
	char* cp=&(s[0]);
	for(int i=0; i<4; i++) std::cout<<*cp++;
}

This is the quickest way to repair this code, although I wouldn't recommend this style.

#include <string>
int main(){
std::string s="abba";
char* cp=&(s[0]);
for(int i=0; i<4; i++) std::cout<<*cp++;

It didn't work for. can you give an example for your first suggestion?

After changing line 33 to ThisOne = &text[0]; the program is compiling and even working.
//edit Other suggestion, C way: include <cstring> and add:

char buff[100]; 
strcpy(buff,text.c_str());
ThisOne=buff;

Edited 5 Years Ago by Zjarek: n/a

To do this in better style, you should do the following two changes:
1) Your line 33 should be:

const char* ThisOne = text.c_str();

And eliminating the declaration of ThisOne, earlier.

2) change all your parameter and return types from "char*" to "const char*".

That should fix it. And you will get null-terminated string from this c_str() function (but the price to pay is that you cannot modify that null-terminated string, and thus, all those "const" keywords to declare that you won't touch the actual values of the characters... which from a quick scan of your code, seems to be the case).

HERE IS ANOTHER I TRIED TO DO PLEASE CHECK THE ATTACHMENT IN THE FIRST POST

/***************************************************
  The following program will take user's input
  and check whether it is in the following language:

  <S> ---> a <B> | <A> | b
  <A> ---> c <A> | c
  <B> ---> d | <A>
****************************************************/


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


char text[200];      // for input storage
char ThisOne;       // for the checking character 


char SProd(char ThisOne ); // <S> --> a <B>  | <A>  | b
char AProd(char ThisOne);   // Grammar 
char XProd(char ThisOne, char x);
char BProd(char ThisOne ); // <B> --> d      | <A>



int main()
{
		 cout<<"Enter a string"<<endl;
		 cin>>text;
		 ThisOne = lexical();
		 SProd();
		 if(ThisOne == '\0')
		   cout<<"valid"<<endl;
		 else
		 cout<<"Invalid"<<endl; 
}


char XProd(char ThisOne, char x)
{
		ThisOne = XProd(ThisOne, 'a');
		ThisOne	 = XProd(ThisOne, 'b');
		ThisOne   = XProd(ThisOne, 'c');
		ThisOne	 = XProd(ThisOne,  'd');
		return ThisOne;
}


char SProd(char ThisOne )
{
	if(ThisOne == 'b')
	{
		ThisOne++;
	}
	else{
		if(ThisOne == 'a')
		{
			ThisOne++;
			BProd(ThisOne);
		}
		else{
			AProd( ThisOne);
		}
	}


char AProd(char ThisOne)
{
  if(ThisOne != 'c' )
  {
	cout<<"Invalid"; 
	exit(1);
  }
  else{
	  ThisOne++; 

  
  while(ThisOne == 'c')
	  ThisOne++
	  return ThisOne
  }
}

   
char BProd(char ThisOne )
{
	if(ThisOne == 'd')
		This++;
		return ThisOne;
	else
		AProd(char ThisOne, char y);
}

That's much much worse than the first program you posted. I mean, the first program is fine. It works except for that tiny one-line mistake. The code you just posted now is far from even compiling.

That's much much worse than the first program you posted. I mean, the first program is fine. It works except for that tiny one-line mistake. The code you just posted now is far from even compiling.

/***************************************************
  The following program will take user's input
  and check whether it is in the following language:

    <S> --->  a <B> | <A> | b
    <A> --->  c <A> |  c
    <B> --->  d | <A>
****************************************************/


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


char* SProd(const char* ThisOne ); // <S> --> a <B>  | <A>  | b
char* AProd(const char* ThisOne ); // <A> --> c <A>  | c
char* BProd(const char* ThisOne ); // <B> --> d      | <A>
char* XProd(const char* ThisOne, const char x);

int main()
{


	string text;
    char* ThisOne;

	 cout<<"Enter a string"<<endl;
	 cin>>text;


    //*ThisOne = text.begin();
     const char* ThisOne = text.c_str();
	 ThisOne = SProd(ThisOne);


	if(*ThisOne== '\0')  //Check whether there is an extra character at the end
		cout<<"valid"<<endl;
	else
		cout<<"Invalid"<<endl;
	system("pause");
   	return 0;
}



char* XProd(const char* ThisOne, const char x)
{
		ThisOne = XProd(ThisOne, 'a');
		ThisOne	 = XProd(ThisOne, 'b');
		ThisOne   = XProd(ThisOne, 'c');
		ThisOne	 = XProd(ThisOne,  'd');
		return ThisOne;
}

 // <S> --> a <B>  | <A>  | b
char* SProd(char* ThisOne )
{
	if (*ThisOne == 'b')
	{
		ThisOne++;
		return ThisOne;
	}

	else {
		if (*ThisOne == 'a')
	{
		ThisOne++;
		 ThisOne = BProd(ThisOne);
		 return ThisOne;
	}
		else
		{
			 ThisOne = AProd(ThisOne);
			 return ThisOne;
		}
	}
}




// <A> --> c <A>  | c
char* AProd(char* ThisOne)
{
	if(*ThisOne != 'c')
	{
		cout<<"Invalid"<<endl;
		exit(1);
	}
	else
	{
		ThisOne++;
		while (*ThisOne == 'c')
			ThisOne++;
			return ThisOne;
	}



}


// <B> --> d      | <A>
char* BProd(char* ThisOne)
{
	if(*ThisOne = 'd')
	{
		ThisOne++;
		return ThisOne;
	}
	else
	{
        AProd(ThisOne);
	}

}

I still got errors:

Compiling: main.cpp
In function `int main()':
: error: conflicting declaration 'const char*ThisOne' 7: error: 'ThisOne' has a previous declaration as `char*ThisOne'
34: error: declaration of `const char*ThisOne'
7: error: conflicts with previous declaration `char*ThisOne'
In function `char* XProd(const char*, char)':
error: invalid conversion from `const char*' to `char*'

Edited 5 Years Ago by Transcendent: n/a

You need to take out the line number 27 (i.e. the previous declaration of ThisOne). And you need to make all your return types "const char*" (not only the parameter types like you did) as in:

const char* XProd(const char* ThisOne, const char x); //notice the const at the start.
//...
const char* XProd(const char* ThisOne, const char x) //notice the const at the start.
{
		ThisOne = XProd(ThisOne, 'a');
		ThisOne	 = XProd(ThisOne, 'b');
		ThisOne   = XProd(ThisOne, 'c');
		ThisOne	 = XProd(ThisOne,  'd');
		return ThisOne;
}

Just do the same for all the other functions.

/***************************************************
  The following program will take user's input
  and check whether it is in the following language:

    <S> --->  a <B> | <A> | b
    <A> --->  c <A> |  c
    <B> --->  d | <A>
****************************************************/


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


char* SProd(const char* ThisOne ); // <S> --> a <B>  | <A>  | b
char* AProd(const char* ThisOne ); // <A> --> c <A>  | c
char* BProd(const char* ThisOne ); // <B> --> d      | <A>
char* XProd(const char* ThisOne, const char x);

int main()
{


	string text;
    //char* ThisOne;

	 cout<<"Enter a string"<<endl;
	 cin>>text;


    //*ThisOne = text.begin();
     const char* ThisOne = text.c_str();
	 ThisOne = SProd(ThisOne);


	if(*ThisOne== '\0')  //Check whether there is an extra character at the end
		cout<<"valid"<<endl;
	else
		cout<<"Invalid"<<endl;
	system("pause");
   	return 0;
}



char* XProd(const char* ThisOne, const char x)
{
		ThisOne = XProd(ThisOne, 'a');
		ThisOne	 = XProd(ThisOne, 'b');
		ThisOne   = XProd(ThisOne, 'c');
		ThisOne	 = XProd(ThisOne,  'd');
		return ThisOne;
}

 // <S> --> a <B>  | <A>  | b
char* SProd(char* ThisOne )
{
	if (*ThisOne == 'b')
	{
		ThisOne++;
		return ThisOne;
	}

	else {
		if (*ThisOne == 'a')
	{
		ThisOne++;
		 ThisOne = BProd(ThisOne);
		 return ThisOne;
	}
		else
		{
			 ThisOne = AProd(ThisOne);
			 return ThisOne;
		}
	}
}




// <A> --> c <A>  | c
char* AProd(char* ThisOne)
{
	if(*ThisOne != 'c')
	{
		cout<<"Invalid"<<endl;
		exit(1);
	}
	else
	{
		ThisOne++;
		while (*ThisOne == 'c')
			ThisOne++;
			return ThisOne;
	}



}


// <B> --> d      | <A>
char* BProd(char* ThisOne)
{
	if(*ThisOne = 'd')
	{
		ThisOne++;
		return ThisOne;
	}
	else
	{
        AProd(ThisOne);
	}

}

main.cpp||In function `char* XProd(const char*, char)':|
|54|error: invalid conversion from `const char*' to `char*'|
||=== Build finished: 1 errors, 0 warnings ===|

You need to take out the line number 27 (i.e. the previous declaration of ThisOne). And you need to make all your return types "const char*" (not only the parameter types like you did) as in:

const char* XProd(const char* ThisOne, const char x); //notice the const at the start.
//...
const char* XProd(const char* ThisOne, const char x) //notice the const at the start.
{
		ThisOne = XProd(ThisOne, 'a');
		ThisOne	 = XProd(ThisOne, 'b');
		ThisOne   = XProd(ThisOne, 'c');
		ThisOne	 = XProd(ThisOne,  'd');
		return ThisOne;
}

Just do the same for all the other functions.

/***************************************************
  The following program will take user's input
  and check whether it is in the following language:

    <S> --->  a <B> | <A> | b
    <A> --->  c <A> |  c
    <B> --->  d | <A>
****************************************************/


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


const char* SProd(const char* ThisOne ); // <S> --> a <B>  | <A>  | b
const char* AProd(const char* ThisOne ); // <A> --> c <A>  | c
const char* BProd(const char* ThisOne ); // <B> --> d      | <A>
const char* XProd(const char* ThisOne, const char x);

int main()
{


	string text;
    //char* ThisOne;

	 cout<<"Enter a string"<<endl;
	 cin>>text;


    //*ThisOne = text.begin();
     const char* ThisOne = text.c_str();
	 ThisOne = SProd(ThisOne);


	if(*ThisOne== '\0')  //Check whether there is an extra character at the end
		cout<<"valid"<<endl;
	else
		cout<<"Invalid"<<endl;
	system("pause");
   	return 0;
}



const char* XProd(const char* ThisOne, const char x)
{
		ThisOne = XProd(ThisOne, 'a');
		ThisOne	 = XProd(ThisOne, 'b');
		ThisOne   = XProd(ThisOne, 'c');
		ThisOne	 = XProd(ThisOne,  'd');
		return ThisOne;
}

 // <S> --> a <B>  | <A>  | b
const char* SProd(char* ThisOne )
{
	if (*ThisOne == 'b')
	{
		ThisOne++;
		return ThisOne;
	}

	else {
		if (*ThisOne == 'a')
	{
		ThisOne++;
		 ThisOne = const char BProd(ThisOne);
		 return ThisOne;
	}
		else
		{
			 ThisOne = const char AProd( ThisOne);
			 return ThisOne;
		}
	}
}

still errors.

at lines 70 and 75, remove the "const char" part, it is not valid C++ syntax.

I did at first but than I had two errors:

main.cpp||In function `const char* SProd(char*)':|
main.cpp|70|error: invalid conversion from `const char*' to `char*'|
main.cpp|75|error: invalid conversion from `const char*' to `char*'|
||=== Build finished: 2 errors, 0 warnings ===|

/***************************************************
  The following program will take user's input
  and check whether it is in the following language:

    <S> --->  a <B> | <A> | b
    <A> --->  c <A> |  c
    <B> --->  d | <A>
****************************************************/


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


const char* SProd(const char* ThisOne ); // <S> --> a <B>  | <A>  | b
const char* AProd(const char* ThisOne ); // <A> --> c <A>  | c
const char* BProd(const char* ThisOne ); // <B> --> d      | <A>
const char* XProd(const char* ThisOne, const char x);

int main()
{


	string text;
    //char* ThisOne;

	 cout<<"Enter a string"<<endl;
	 cin>>text;


    //*ThisOne = text.begin();
     const char* ThisOne = text.c_str();
	 ThisOne = SProd(ThisOne);


	if(*ThisOne== '\0')  //Check whether there is an extra character at the end
		cout<<"valid"<<endl;
	else
		cout<<"Invalid"<<endl;
	system("pause");
   	return 0;
}



const char* XProd(const char* ThisOne, const char x)
{
		ThisOne = XProd(ThisOne, 'a');
		ThisOne	 = XProd(ThisOne, 'b');
		ThisOne   = XProd(ThisOne, 'c');
		ThisOne	 = XProd(ThisOne,  'd');
		return ThisOne;
}

 // <S> --> a <B>  | <A>  | b
const char* SProd(char* ThisOne )
{
	if (*ThisOne == 'b')
	{
		ThisOne++;
		return ThisOne;
	}

	else {
		if (*ThisOne == 'a')
	{
		ThisOne++;
		  ThisOne =   BProd(ThisOne);
		 return ThisOne;
	}
		else
		{
		  ThisOne =   AProd( ThisOne);
			 return ThisOne;
		}
	}
}




// <A> --> c <A>  | c
const char* AProd(char* ThisOne)
{
	if(*ThisOne != 'c')
	{
		cout<<"Invalid"<<endl;
		exit(1);
	}
	else
	{
		ThisOne++;
		while (*ThisOne == 'c')
			ThisOne++;
			return ThisOne;
	}



}


// <B> --> d      | <A>
const char* BProd(char* ThisOne)
{
	if(*ThisOne = 'd')
	{
		ThisOne++;
		return ThisOne;
	}
	else
	{
        AProd(ThisOne);
	}

}

You forgot to change all the types to const char*:

/***************************************************
  The following program will take user's input
  and check whether it is in the following language:

    <S> --->  a <B> | <A> | b
    <A> --->  c <A> |  c
    <B> --->  d | <A>
****************************************************/


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


const char* SProd(const char* ThisOne ); // <S> --> a <B>  | <A>  | b
const char* AProd(const char* ThisOne ); // <A> --> c <A>  | c
const char* BProd(const char* ThisOne ); // <B> --> d      | <A>
const char* XProd(const char* ThisOne, const char x);

int main()
{


	string text;

	 cout<<"Enter a string"<<endl;
	 cin>>text;


    //*ThisOne = text.begin();
     const char* ThisOne = text.c_str();
	 ThisOne = SProd(ThisOne);


	if(*ThisOne== '\0')  //Check whether there is an extra character at the end
		cout<<"valid"<<endl;
	else
		cout<<"Invalid"<<endl;
	system("pause");
   	return 0;
}



const char* XProd(const char* ThisOne, const char x)
{
		ThisOne = XProd(ThisOne, 'a');
		ThisOne	 = XProd(ThisOne, 'b');
		ThisOne   = XProd(ThisOne, 'c');
		ThisOne	 = XProd(ThisOne,  'd');
		return ThisOne;
}

 // <S> --> a <B>  | <A>  | b
const char* SProd(const char* ThisOne ) //notice const here.
{
	if (*ThisOne == 'b')
	{
		ThisOne++;
		return ThisOne;
	}

	else {
		if (*ThisOne == 'a')
	{
		ThisOne++;
		  ThisOne =   BProd(ThisOne);
		 return ThisOne;
	}
		else
		{
		  ThisOne =   AProd( ThisOne);
			 return ThisOne;
		}
	}
}




// <A> --> c <A>  | c
const char* AProd(const char* ThisOne) //notice const here.
{
	if(*ThisOne != 'c')
	{
		cout<<"Invalid"<<endl;
		exit(1);
	}
	else
	{
		ThisOne++;
		while (*ThisOne == 'c')
			ThisOne++;
			return ThisOne;
	}



}


// <B> --> d      | <A>
const char* BProd(const char* ThisOne) //notice const here.
{
	if(*ThisOne = 'd')
	{
		ThisOne++;
		return ThisOne;
	}
	else
	{
        AProd(ThisOne);
	}

}

Well, on line 107, you have an assignment that should be an equality comparison (i.e. two equal signs). Besides that, the program I posted on my last post compiles perfectly well on a standard compiler (gcc).

I run this on visal c++ 2010 and code::block but I got errors. This what I have:

/***************************************************
  The following program will take user's input
  and check whether it is in the following language:

    <S> --->  a <B> | <A> | b
    <A> --->  c <A> |  c
    <B> --->  d | <A>

valid                   invalid
c cc ccc b a        ab acd ada
ad ac acc accc      ba cd ca da

****************************************************/


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


const char* SProd(const char* ThisOne ); // <S> --> a <B>  | <A>  | b
const char* AProd(const char* ThisOne ); // <A> --> c <A>  | c
const char* BProd(const char* ThisOne ); // <B> --> d      | <A>
const char* XProd(const char* ThisOne, const char x);

int main()
{


	string text;
    //char* ThisOne;

	 cout<<"Enter a string"<<endl;
	 cin>>text;


    //*ThisOne = text.begin();
     const char* ThisOne = text.c_str();
	 ThisOne = SProd(ThisOne);


	if(*ThisOne== '\0')  //Check whether there is an extra character at the end
		cout<<"valid"<<endl;
	else
		cout<<"Invalid"<<endl;
	system("pause");
   	return 0;
}



const char* XProd(const char* ThisOne, const char x)
{
		ThisOne = XProd(ThisOne, 'a');
		ThisOne	 = XProd(ThisOne, 'b');
		ThisOne   = XProd(ThisOne, 'c');
		ThisOne	 = XProd(ThisOne,  'd');
		return ThisOne;
}

 // <S> --> a <B>  | <A>  | b
const char* SProd(char* ThisOne )
{
	if (*ThisOne == 'b')
	{
		ThisOne++;
		return ThisOne;
	}

	else {
		if (*ThisOne == 'a')
	{
		ThisOne++;
		  ThisOne =   BProd(ThisOne );
		 return ThisOne;
	}
		else
		{
		  ThisOne =   AProd( ThisOne);
			 return ThisOne;
		}
	}
}




// <A> --> c <A>  | c
const char* AProd(const char* ThisOne)
{
	if(*ThisOne != 'c')
	{
		cout<<"Invalid"<<endl;
		exit(1);
	}
	else
	{
		ThisOne++;
		while (*ThisOne == 'c')
			ThisOne++;
			//return ThisOne;
	}



}


// <B> --> d      | <A>
const char* BProd(const char* ThisOne)
{
	if(*ThisOne == 'd')
	{
		ThisOne++;
		return ThisOne;
	}
	else
	{
        AProd(ThisOne);
	}

}

hey, I can seem to make this one work so I wrote another one. It runs but it doesn't work. I tested this but it keep saying invalid for everything anyway. You can check the flow chart again; i just changed ThisOne to ToBeChecked.

valid invalid
c cc ccc b a ab acd ada
ad ac acc accc ba cd ca da

here is the link to the flowchart for people that don't want to download it from the attachment:

main function: http://img84.imageshack.us/img84/6103/mainfunction.png

SProd Function:http://img269.imageshack.us/img269/2108/sprodfunction.png

AProd Function: http://img62.imageshack.us/img62/9852/aprodfunction.png

BProd Function: http://img51.imageshack.us/img51/3536/bprodfunction.png

/*
Write a recursive descent parser for the following grammar.
Specifically, your C & C++ program will let user enter any string up to 200 characters. 
Also, the program will tell user whether the input string satisfies the following grammar.

    <S> --->  a <B> | <A> | b
    <A> --->  c <A> |  c
    <B> --->  d | <A>

    Non-terminals are in "< >" brackets, while lower case characters and numbers are terminals.

    The following is a set of sample data for testing the first set of grammar:
     
		valid 							invalid
    c 	cc 	ccc 	b 				a 	ab 	acd 	ada
    ad 	ac 	acc 	accc 			ba 	cd 	ca 	da
*/

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

char text[200];
char ToBeChecked;

char lexical(); //identify the characters
void SProd();// <S> --->  a <B> | <A> | b
void AProd();// <A> --->  c <A> |  c
void BProd();// <B> --->  d | <A>


int main()
{
    cout<<"Enter a string(max. 100 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; everytime move on to next one
    index++; //update index
    return text[index]; //return the value of index
}


//<S> --->  a <B> | <A> | b
void SProd()
{
    if(ToBeChecked == 'b')
	{
		ToBeChecked =lexical();
	}
	else
	{
	if(ToBeChecked == 'a')
	{
			ToBeChecked = lexical();
			BProd(); 
	}
		else
		{
			AProd();
			}
	}

}

// <A> --->  c <A> |  c
void AProd()
{
   if(ToBeChecked != 'c')
   {
	   cout<<"Invalid"<<endl;
		 exit(1);
   }
   else
	   ToBeChecked = lexical();

   while(ToBeChecked == 'c')
	   ToBeChecked = lexical();
}



//<B> --->  d | <A>
void BProd()
{
    
	if(ToBeChecked == 'd')
		ToBeChecked = lexical();
	else
		AProd(); 
	

}

Line 40 should have a _double_ equal sign "==". Watch out for typos like that. And turn on all the warning when you compile! -Wall or /Wall depending on your compiler.

Line 40 should have a _double_ equal sign "==". Watch out for typos like that. And turn on all the warning when you compile! -Wall or /Wall depending on your compiler.

I'm using eclipse c++/java, visual c++, code::block. What do you mean -Wall or /Wall?

-Wall stands for -W (warning) and all (for "all"). It just is a command-line argument to the compiler to tell it to report all warnings about your code. If you don't enable this, the compiler will only report errors that prevent it from compiling the code. If you enable all the warnings, it will report many things that are indications of possible mistakes that you could have made (e.g. using single = (assignment operator) in an if-statement, casting a double variable to an int (that will result in a loss of information), and many other little things that can come from either typos or poor coding practices (like C-style casts)). This is a general rule for good programming practices: "enable all warnings and treat them as if they were errors" (i.e. fix your code such that the compiler doesn't complain about anything).

When you are using an IDE (eclipse, VC++, Code.Blocks), you typically enable all the warnings via the "build options" for your project (in the menus, find "build option" or something similar, and then you should be able to find a large list of "compiler options" from which there are several options that relate to warnings, they pretty much should all be enabled (checked)).

Personally, I don't understand why all warnings are not enabled by default, because they should (i.e. it should be that compiler options are used to disable warnings that are all enabled by default).

This article has been dead for over six months. Start a new discussion instead.