Hey everyone, this program's been killing me. I have to create a program that takes another program as input and counts the lines. Pretty simple, except that to do this, you have to count semicolons which means you have to negate semicolons that are comment text or within double quotes. My plan was to simply read the string until you reach the double quote and change everything after the double quote to a NULL character, and I wrote what I believe to be a mostly sound piece of code to do this:

for(counter = 0; counter < 500; counter++)
{	
	if (select == 0 && buffer[counter] == '"')
	{
		select = 1;
		store = buffer[counter];
	}
	if (select == 1 && buffer[counter] != '"')
        {
	         buffer[counter] = NULL;
	}
	if (select == 1 && buffer[counter] == '"' && buffer[counter] != store)
	{	
		select = 0;
	}
        }

The point of this piece of code is to letter by letter compare each one to the ". Initially, with select set to 0, nothing will change. When the program first encounters a " in the string, select will change to 1, and from there the program will convert every character to a null character until it reaches the next ", at which point, it will set select back to zero. It doesn't appear to be working, though. Anything look wrong with this?

Recommended Answers

All 4 Replies

Why? When you see your first " stop looking for ; and look for a " instead. When you find the ", continue looking for ;.

Why NULL out any char's?

Your buffer[index] is currently OUTSIDE a parenthesis or double quote, or And you program should start as OUTSIDE.

Your buffer[index] is currently INSIDE a parenthesis or double quote. Both cases have to be dealt with. When buffer[index] goes INSIDE, then match equal's ')' or '"' char. When buffer index meets that char next, then it's status will switch to OUTSIDE again.

if(your status is OUTSIDE), and your buffer[index] == ';', then increment your line count.

In a program like this, less is more. Don't have your program do things that obfuscate your goals.

Give each state a name, not some meaningless number like 0 or 1

typedef enum {
  CH_NORMAL,
  CH_IN_SINGLE_QUOTE,       // found a ', skip until another ', but watch for \'
  CH_IN_DOUBLE_QUOTE,       // found a ", skip until another ", but watch for \"
  CH_IN_OMMENT_START_OR_DIV,// found a /, could be /* or //, but might be just division
  CH_IN_C_COMMENT,          // found /*, now skip to */
  CH_IN_CPP_COMMENT,        // found //, now skip to end of line
} ch_state;

ch_state state = CH_NORMAL;
switch ( state ) {
  case CH_NORMAL:
    if ( ch == '\'' ) state = CH_IN_SINGLE_QUOTE;
    // do other things associated with this state
    break;
  case CH_IN_SINGLE_QUOTE:
    if ( ch == '\'' ) state = CH_NORMAL;
    break;
}

Read about state machines
http://en.wikipedia.org/wiki/Finite_State_Machine

commented: Excellente! +8

Much thanks guys! Spent a good few hours redoing it to something more similar to that and it appears to work pretty well now! Thanks a ton!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.