7 Replies - 1894 Views - Last Post: 19 May 2007 - 06:39 PM Rate Topic: -----

#1 enpey   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Why does my program halt here?

Posted 16 May 2007 - 03:22 AM

Hey all,

Ok so I wrote this code in MS Visual Studio, and I have now copied it over to a UNIX server and am altering it a little but am getting stuck when running the program.
The program reads from std input and outputs to display, all comments within a file (we're eventually helping our lecturer to spell check all his comments as some of it isn't so good) between the /* and */ comment delimiters. It works fine up until the last line, so I've thrown a printf in to make sure my first and last variables (which are the first comment character index and the last comment character index respectively) are correct and it seems they are. The problem is it stops directly after the space in the format string in this line -
 printf ("\ne%4d. ", j); 


 if (first - last != 0)
 {
		 printf ("%d,%d",first,last);
		 printf ("\ne%4d. ", j);

		 for (i=first; i<last; i++)
		 {
				 printf ("%c", string[i]);
		 }
 };




Any ideas?

Thanks, Nathan


Edit: With one file I am trying to read comments from, on the final commented line, the first and last variables store:-

first: 9
last: 26

This post has been edited by enpey: 16 May 2007 - 04:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Why does my program halt here?

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6698
  • View blog
  • Posts: 31,070
  • Joined: 10-May 07

Re: Why does my program halt here?

Posted 16 May 2007 - 05:31 AM

 if (first - last != 0)
 {
		 printf ("%d,%d",first,last);
		 printf ("\ne%4d. ", j);

		 for (i=first; i<last; i++)
		 {
				 printf ("%c", string[i]);
		 }
 };



What if you just did...
if(first!=last)


Also, I would take the ';' off your last line, after the if
Was This Post Helpful? 0
  • +
  • -

#3 enpey   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Re: Why does my program halt here?

Posted 16 May 2007 - 04:10 PM

The if statement isn't what's stopping my program... As it runs through
		  printf ("%d,%d",first,last);
		 printf ("\ne%4d. ", j);
but it pauses directly after this fullstop followed by the space... I am completely stumped as in this case

.
.
.
e 66. Output for only beginning found12,33
e 77. Output for end found9,26
e 92.


where first is 9 and last is 26....
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: Why does my program halt here?

Posted 16 May 2007 - 10:03 PM

You must remember that C/C++ is different than most languages, as the place where it stops outputting values, is not necessarily where the error occurred, but it is usually close to it.

-without seeing the rest of the code, it is difficult to be certain, but be sure that last is not larger than the size of your array.
-I would also like to suggest, you use:
if(last - first > 0)
this will ensure that last is actually larger than first, as well as handling your 0 case. Just for a better algorithm.
-it is possible the error is generated elsewhere, in a call or after the loop, to be sure, posting your entire code would be helpful
Was This Post Helpful? 0
  • +
  • -

#5 enpey   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Re: Why does my program halt here?

Posted 19 May 2007 - 04:47 PM

Here is the entire program.. I have no more idea than I did a week ago..

#include <stdio.h>

int main (void)
{
	int i, j, sent, first, last, endcase, cfound;
	char string [111];

	j=1;

	sent = 0;	/* Sentinel to ensure loop to be run until Ctrl + Break pressed */
	while (sent != 1)
	{
		gets (string);
		/* Initializing variables for each line */
		cfound = 0;
		first = 0;
		last = 0;
		i=0;	
		
		while (string[i] != '\0')
		{
			if (string[i] == '/')
			{
				if (string[i+1] == '*')
				{
					/* Found the beginning of a comment */
					endcase = 0;
					i++;
					first = i + 1; /* Position of first comment character in line */
				}
			}

			if (string[i] == '*' && endcase != 1)
			{
				if (string[i+1] == '/')
				{
					/* Found end of a comment */
					endcase = 1;
					last = i - 1; /* Position of last comment character in line */
					i++;
				}
			}

			i++;
		}

		switch (endcase)
		{
		/* Output comments found */

		case 0: /* Output for only beginning found */
			printf ("\nb%4d.", j);

			i = first;
			while (string[i] != '\0')
			{
				printf ("%c", string[i]);
				i++;
			}
			break;

		case 1: /* Output for end found */
			if (last - first > 0)
			{
								printf (" (%d,%d)",first,last);
				printf ("\ne%4d.", j);
				for (i=first; i<last; i++)
				{
					printf ("%c", string[i]);
				}
			}
			break;
		}
			
		

		j++; /* Next line number */
	}

	return 0;
}




Thanks, Nathan
Was This Post Helpful? 0
  • +
  • -

#6 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Why does my program halt here?

Posted 19 May 2007 - 06:05 PM

Need to fix an off-by-one error in your code when outputting the comments. You need to include the equals sign in the FOR loop.
for (i=first; i<=last; i++)

The reason it is stopping is it is waiting for input from the user. All the loops completed successfully and it is back at the top of the first while loop at the following line of code.
gets (string);


Is this not what it is supposed to do??

You need to keep entering in comment lines for it to keep running through the loops. Here is a screenshot of it in action.

If this is not the output you are expecting, then describe what you are expecting for output. Include the input that you are entering into your program in your post.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 enpey   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Re: Why does my program halt here?

Posted 19 May 2007 - 06:27 PM

Yes that is correct in regards to what I am expecting it to do. But I am using input redirection so when I compile it i would type

$ codeStripper < codeStripper.c

where codeStripper is the binary program, and codeStripper.c is my source file. When I run this it gives me this output:

bash-3.00$ a.out < codeStripper.c

e 25. Sentinel to ensure loop to be run until Ctrl + Break pressed (4,41)
e 29. Initializing variables for each line (7,40)
e 41. Found the beginning of a comment (22,66)
e 44. Position of first comment character in line (7,30)
e 52. Found end of a comment (21,64)
e 54. Position of last comment character in line (4,26)
e 64. Output comments found (12,44)
e 66. Output for only beginning found (12,33)
e 77. Output for end found (9,26)
e 92.


And as you said, the cursor waits just after the e 92.

This would be slightly different to the provided file, as my file has a header with name and date information.

And with the 'i <= last' instead of 'i < last', that is only the last character in the comment, which in my programs is always a space. If I adjust that it will only output one space after each line for the file codeStripper.c, but it finishes in exactly the same way.

Thanks, Nathan
Was This Post Helpful? 0
  • +
  • -

#8 enpey   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Re: Why does my program halt here?

Posted 19 May 2007 - 06:39 PM

It is something to do with the last comment in the file. As if you add a comment after the

/* Next line number */

comment it will output the above comment without problem, but again not the last comment.

And in the initial problem without the additional comment, the above comment is not on the last line of the file, and so the program would still be expected to be running through the remainder of the file...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1