Please help! My program hangs when calling function, can you tell me why?

This is a discussion on Please help! My program hangs when calling function, can you tell me why? within the C Programming forums, part of the General Programming Boards category; I haven't been able to test the logic inside the function yet (which I know isn't complete yet) as my ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    14

    Please help! My program hangs when calling function, can you tell me why?

    I haven't been able to test the logic inside the function yet (which I know isn't complete yet) as my program just hangs when it try to call substrings().

    I have lots of messages printing to try and debug. I am only just starting to learn C so apologise for my inelegant code.

    Any help to get me past this hurdle would be very much appreciated. Thank you in advance.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define TRUE 1
    #define NUM_OPTION_STATS 5
    #define MAX_SUB_STRING_INPUT 20
    
    int substrings( int *optionStats, char *source, char *subString);
    
    int main(void){
       
       int optionStats[NUM_OPTION_STATS];
       int i=0, j=0;
       char source[MAX_SUB_STRING_INPUT] = {0};
       char subString[MAX_SUB_STRING_INPUT] = {0};
       
       char in;
       int finished1 = 0;
       int finished2 = 0;
       
       printf("Substrings\n");
       printf("-----------\n");
       
       do {
          printf("Enter a string (1-20 characters): ");
          
          while ((in = getchar())!= '\n') {
             source[i++] = in;
          }
          
          source[i] = '\0';
          
          if (i=0)
          {
             fputs("You haven't entered anything. Please try again. \n", stderr);
             
          }
          else {
             
             finished1=1;
             
          }
          
       } while (finished1==0);
       
       printf("should have worked\n");
       printf("check source: %s\n", source);
       i=0;
       while (source[i] != '\0') {
          putchar(source[i++]);
       }
       printf("\nMove onto substring\n");
       
       
       
       do {
          printf("Enter a substring (1-20 characters): ");
          
          for (j=0;  (in = getchar()) != '\n'; ++j) {
             printf("%c\n", in);
             subString[j] = in;
             printf("%c\n", subString[j]);
             
          }
          subString[j] = '\0';
          if (j=0)
          {
             fputs("You haven't entered anything. Please try again. \n", stderr);
             
          }
          
          if (j<=MAX_SUB_STRING_INPUT) {
             finished2=1;
          }
          
       } while (finished2==0);
       
       printf("should have worked\n");
       printf("check substring: %s\n", subString);
       printf("check substring 1st character: %c\n", subString[0]);
       
       printf("About to call function\n");
       printf("Let's check source again first: %s\n", source);
       
       substrings(optionStats, source, subString); //this is where it hangs
       printf("Function called\n");
       
    }
    
    
    
    
    /****************************************************************************
     * Function substrings() is an implementation of the strstr() standard library
     * function.
     ****************************************************************************/
    int substrings(int *optionStats, char* source, char* subString)
    {
       printf("You made it into the substring function!");
       int i, j, k;
       int source_length, substring_length;
       
       /*determine length of source string*/
       for (i=0; i<MAX_SUB_STRING_INPUT; i++) {
          while (source[i] != '\0') {
             source_length++;
          }             
       }
       /*TEST*/
       printf("%d\n", source_length);
       
       /*determine length of substring string*/
       for (i=0; i<MAX_SUB_STRING_INPUT; i++) {
          if (subString[i] != '\0')
             substring_length++;
       }
       
       /*TEST*/
       printf("%d\n", substring_length);
       
       
       for (j=0; j<substring_length; j++) {
          for (k=0; k<source_length-substring_length; k++) {
             if (source[k] && subString[j] == 0) {
                j++;
             }
          }
       }
       
       if (j == substring_length) {
          printf("Output: \"%s\" is a substring of \"%s\"", subString, source);
       }
       else {
          printf("Output: \"%s\" is NOT a substring of \"%s\"", subString, source);
       }
       
       
       optionStats[3]++;
       
       return TRUE;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    As first attempts go, it's a pretty good effort - keep at it

    Consider using a debugger rather than printf's.
    Eg.
    Code:
    $ gcc -g -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:32: warning: suggest parentheses around assignment used as truth value
    bar.c:65: warning: suggest parentheses around assignment used as truth value
    bar.c:87: warning: control reaches end of non-void function
    $ gdb ./a.out 
    GNU gdb (GDB) 7.1-ubuntu
    Copyright (C) 2010 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "i486-linux-gnu".
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>...
    Reading symbols from /home/sc/work/a.out...done.
    (gdb) run
    Starting program: /home/sc/work/a.out 
    Substrings
    -----------
    Enter a string (1-20 characters): hello
    should have worked
    check source: hello
    hello
    Move onto substring
    Enter a substring (1-20 characters): ll
    l
    l
    l
    l
    should have worked
    check substring: ll
    check substring 1st character: l
    About to call function
    Let's check source again first: hello
    ^C
    Program received signal SIGINT, Interrupt.
    0x08048837 in substrings (optionStats=0xbffff3ec, source=0xbffff428 "hello", subString=0xbffff414 "ll") at bar.c:104
    104	      while (source[i] != '\0') {
    See, I just let it run then pressed ctrl-c to break into it, knowing that most likely it will be in a loop that it can't get out of.

    Look at this while loop very carefully, i does NOT change inside the loop, and neither does source[i]
    So in effect, you have a while(TRUE) statement.

    Your later attempt at strlen() uses an if statement, and this seems a lot better.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Thank you for your prompt response Salem.

    So my problem is in the function, not in main? I thought because it hadn't printed the inside function message it hadn't even made it there.

    I will think about your comments and have another go. I might be back with another question! thanks again.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look closely... what does this section of code do to your loop's exit condition?

    Code:
         else {
            finished1=1;
          }
       } while (finished1==0);

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Hi Tater,

    Thanks for your assistance.

    I'm sorry, but I can't see the problem with this. I thought by setting finished1 to 1, I am not meeting while condition of finished 1 being equal to 0 and therefore breaking out of the loop.

    Is this reasoning incorrect?

    (BTW I have now gotten this called function to work correctly and I am trying to implement input validation. So your comment is timely!)

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > Look closely... what does this section of code do to your loop's exit condition?
    It seemed OK to me - perhaps you meant the if ( i = 0 ) a few lines earlier?

    @cstuggle - see the warning messages in my compilation - "suggest parentheses around assignment used as truth value"
    Normally, when you write if ( i = 0 ), you normally want if ( i == 0 )

    But if you REALLY want the assignment, then you write if ( (i = 0) )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Yep you are right Salem - I was wanting to compare not assign there. I noticed this working through the input validation and have fixed, silly mistake!
    Cheers again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. having some trouble calling a function in the main program
    By CMakesMeSad :( in forum C Programming
    Replies: 19
    Last Post: 06-26-2009, 09:33 PM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  3. Program hangs
    By abh!shek in forum C Programming
    Replies: 4
    Last Post: 05-25-2008, 08:02 PM
  4. program hangs after execution
    By nizbit in forum C Programming
    Replies: 6
    Last Post: 04-16-2005, 09:52 AM
  5. program hangs during runtime
    By nizbit in forum C Programming
    Replies: 8
    Last Post: 02-19-2005, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21