hi all,
i am really stuck with c++ pointer issues.
what my program is supposed to do is to take input from user and recognize whether the entered string is a valid word from the grammer S::= a|(S)
hence grammer should accept words like...
a, (a), ((a)),(((a)))....
i have done a bit of work on it, but my code seems to just stuck in an infinite loop or something. i think the error might lie in the pointer increments, but just cant place it rite.
have a look at code(its half done)
thanks in advance

#include <iostream.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>

void recognizer();
void checkValidWord();
char word[]="\0";
char *wordptr;
int openBraceCount=0,closeBraceCount=0;

void main(){

clrscr();
cout<<" Enter word"<<"\n";
cin>>word;
wordptr=&word[0];
cout<<word;
checkValidWord();
getche();

}

void checkValidWord(){

int length;
length= strlen(word);

if(length == '2' && word[0] == 'a' && word[1] == '\0')
	cout<<" \n Valid Word ";

if(length % 2==0)
	cout<<" \n Invalid Word ";

recognizer();
if(openBraceCount == closeBraceCount)
	cout<<" \n Valid Word ";

getche();
}


void recognizer(){

while( *wordptr != '\0')
{
  if(strchr("a", *wordptr)){
	wordptr=wordptr++;
	recognizer();
	}

  else{
   if(strchr("(", *wordptr)){
	openBraceCount += 1;
	wordptr=wordptr++;
	recognizer();
	}
       }
   if(strchr(")", *wordptr)){
	closeBraceCount += 1;
	wordptr=wordptr++;
	recognizer();
	}
	}


}

I would step back from the actual implementation and look at you algorithm. Whatever your algorithm is, it isn't obvious to me. Why bother with pointers at all? The [] operator works like a pointer in your case and it makes it easier to read.

Lines 32 and 33 is a good test. Anything with an even length is invalid. Line 29 has a lot of problems. If your base case is simply testing for the word "a", just test for the word "a" using strcmp. There's no need to parse through it and there are problems with the parsing. '2' should not be in quotes. That makes it a character. You want it as a number. And if strlen returns 2, you already know exactly where the null terminator is (index 2, not index 1), so don't test for it. It is impossible for line 29 to evaluate as true.

You should step back from the problem and think about how you would solve it as a human being not using a computer. Then put that solution into code.

Edited 6 Years Ago by VernonDozier: n/a

I'm really confused, now. Is this valid:

char word[]="\0";
cin>>word;

No. This will compile, but I imagine it will lead to a segmentation fault. You have not reserved any space for the word[] array. This is better:

char word[100];
cin >> word;

If you type 99 or fewer characters, you are fine because it has enough space to store everything. Type 100 or more characters and you have overflowed your storage (the '\0' takes up a space) and you have a problem.

You can avoid all pointers and storage problems by simply using C++-style strings inside of C-style strings:

#include <string>
#include <iostream>
using namespace std;

int main ()
{
    string firstName;
    cout << "Enter your first name : ";
    cin >> firstName;
    cout << "Your first name is " << firstName << endl;
    return 0;
}

Yeah, I know about std::string.
I took the example from gillanism's listing. It compiled and even worked, but I guess it was just a luck. Just wanted be to sure, so I asked.
Thx for the answer.

Why bother with pointers at all? The [] operator works like a pointer in your case and it makes it easier to read.

Can you kindly explain this line
thanks in advance :)

Edited 6 Years Ago by gillanism: n/a

Can you kindly explain this line
thanks in advance :)

I'm saying change wrdptr to an integer and use it as an array index:

#include <iostream.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>

void recognizer();
void checkValidWord();
char word[]="\0";
char *wordptr;
int openBraceCount=0,closeBraceCount=0;

void main(){

clrscr();
cout<<" Enter word"<<"\n";
cin>>word;
wordptr=&word[0];
cout<<word;
checkValidWord();
getche();

}

void checkValidWord(){

int length;
length= strlen(word);

if(length == '2' && word[0] == 'a' && word[1] == '\0')
	cout<<" \n Valid Word ";

if(length % 2==0)
	cout<<" \n Invalid Word ";

recognizer();
if(openBraceCount == closeBraceCount)
	cout<<" \n Valid Word ";

getche();
}


void recognizer(){

while( *wordptr != '\0')
{
  if(strchr("a", *wordptr)){
	wordptr=wordptr++;
	recognizer();
	}

  else{
   if(strchr("(", *wordptr)){
	openBraceCount += 1;
	wordptr=wordptr++;
	recognizer();
	}
       }
   if(strchr(")", *wordptr)){
	closeBraceCount += 1;
	wordptr=wordptr++;
	recognizer();
	}
	}


}

Line 9 : change the declaration to:

int wordptr;

Line 17: initialize to the beginning of the word:

wordptr=0;

Now change lines like line 45 to something like this:

while (word[wordptr] != '\0')

Lines 47 to 50 would be something like this:

if(word[wordptr] == 'a') {
	wordptr++;
	recognizer();
}

Basically replace *wordptr with word[wordptr] everywhere. It's six of one, half a dozen of the other, but you are having some trouble understanding how pointers work and you are pretty much only comparing single characters anyway. I just think it looks cleaner using the [] operator.

Note that if you have a char* called word and the contents are "Hello World", both of these point to the same memory location and are equivalent:

char a = *(word + 8);
char b = word[8];

I think the second looks cleaner than the first. Both a and b will contain the letter 'r'.

The program has some other problems too, but I think this will help take away the pointer aspect of it.

I'm saying change wrdptr to an integer and use it as an array index:

#include <iostream.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>

void recognizer();
void checkValidWord();
char word[]="\0";
char *wordptr;
int openBraceCount=0,closeBraceCount=0;

void main(){

clrscr();
cout<<" Enter word"<<"\n";
cin>>word;
wordptr=&word[0];
cout<<word;
checkValidWord();
getche();

}

void checkValidWord(){

int length;
length= strlen(word);

if(length == '2' && word[0] == 'a' && word[1] == '\0')
	cout<<" \n Valid Word ";

if(length % 2==0)
	cout<<" \n Invalid Word ";

recognizer();
if(openBraceCount == closeBraceCount)
	cout<<" \n Valid Word ";

getche();
}


void recognizer(){

while( *wordptr != '\0')
{
  if(strchr("a", *wordptr)){
	wordptr=wordptr++;
	recognizer();
	}

  else{
   if(strchr("(", *wordptr)){
	openBraceCount += 1;
	wordptr=wordptr++;
	recognizer();
	}
       }
   if(strchr(")", *wordptr)){
	closeBraceCount += 1;
	wordptr=wordptr++;
	recognizer();
	}
	}


}

Line 9 : change the declaration to:

int wordptr;

Line 17: initialize to the beginning of the word:

wordptr=0;

Now change lines like line 45 to something like this:

while (word[wordptr] != '\0')

Lines 47 to 50 would be something like this:

if(word[wordptr] == 'a') {
	wordptr++;
	recognizer();
}

Basically replace *wordptr with word[wordptr] everywhere. It's six of one, half a dozen of the other, but you are having some trouble understanding how pointers work and you are pretty much only comparing single characters anyway. I just think it looks cleaner using the [] operator.

Note that if you have a char* called word and the contents are "Hello World", both of these point to the same memory location and are equivalent:

char a = *(word + 8);
char b = word[8];

I think the second looks cleaner than the first. Both a and b will contain the letter 'r'.

The program has some other problems too, but I think this will help take away the pointer aspect of it.

oh yes..
hey thanks a lot for your guidance
i am definitely gonna try this :)

This question has already been answered. Start a new discussion instead.