This program keeps crashing before it is over

Discussion in 'C' started by bold-ally, Apr 25, 2009.

  1. bold-ally

    bold-ally New Member

    Joined:
    Apr 25, 2009
    Messages:
    8
    Likes Received:
    1
    Trophy Points:
    0
    This program keeps crashing before it is over .
    If only get to the word eutc then it crashes !
    How do i prevent it from crashing ?



    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    void refill() ;
    void fill ( ) ;
    void testif ( ) ;
    void addchar ( ) ;
    void filla ( ) ;
    
    int i = 0 ;
    int hold ;
    char array[50]  ;
    int num ; 
    int    line ;
    const int maxchar = 7 ;
    ofstream File;
    
    int main ()
    {
        File.open("File.txt",ios::out);
        line = 0 ;
        num = 0 ;
        array[1] = '\0' ;
        fill ( ) ;
        File.close();
    
        return 0 ;
    
    }
    
    void fill (  ) 
    {
        if ( line < maxchar )
        {
    
        array[num] = 'a' ;
        array[num +1] = '\0' ;
        for ( i = 0 ; i < 25 ; i++ )
        {
            File << array << " " ;
            array[num]++ ;    
        }
            File << array << " " ;
        testif ( ) ;
        }
    }
    
    void testif ( ) 
    {
        if ( line < maxchar ) {    
        if ( line == 0)
            addchar( ) ;
        if ( array [line -1 ] == 'z' ){
            line-- ;
            testif ( ) ;
        }
        else {
            array [line - 1 ] ++ ;
            hold = line ;
            refill () ;
            filla() ;
        }
            testif ( ) ;
        }
    }
    
    void addchar () 
    {
        if ( line < maxchar ) 
        {
            num ++ ;
            line = num ;
            array [num +1] = '\0' ;
            for ( i = 0 ; i < num ; i++ )
                array[i] = 'a' ;
            filla( ) ;
        }
    
    }
    
    void filla ( ) 
    {
        if ( line < maxchar )
        {
        array[num] = 'a' ;
        array[num +1] = '\0' ;
        for ( i = 0 ; i < 25 ; i++ )
        {
            File << array << " " ;
            array[num]++ ;    
        }
            File << array << " " ;
        testif ( ) ;
        }
    }
    
    void refill()
    {
        if ( line < maxchar )
        {
    
            for ( hold ; hold <= num ; hold++ ) {
                array[hold] = 'a' ; 
                }
            line  = num ;
            filla() ;
        }
    }
     
    Last edited by a moderator: Apr 26, 2009
  2. bold-ally

    bold-ally New Member

    Joined:
    Apr 25, 2009
    Messages:
    8
    Likes Received:
    1
    Trophy Points:
    0
    I forgot to say what this program does .
    It is simply a dictionary builder that registers the words in a file it is supposed to go from a till zzzzzzz but it keeps crashing on eutc
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    eutc isn't a word, could that be why it's crashing?
    Code's a lot easier to read if you post it in code blocks. Keeps the formatting that way.
     
  4. bold-ally

    bold-ally New Member

    Joined:
    Apr 25, 2009
    Messages:
    8
    Likes Received:
    1
    Trophy Points:
    0
    It can't be that ! LoL !
    Consider this code a huge for loop that will do the following

    aaaa aaab aaac .... ggga gggb ... untill zzzz
    So the fact that where it crashes is not a word makes no difference .
    It has to do more with the way the code is written
    I just have no clue how to refine it and make it simpler and easier on the pc to compile .
     
  5. bold-ally

    bold-ally New Member

    Joined:
    Apr 25, 2009
    Messages:
    8
    Likes Received:
    1
    Trophy Points:
    0
    ^^
    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    void refill() ;
    void fill ( ) ;
    void testif ( ) ;
    void addchar ( ) ;
    void filla ( ) ;
    
    int i = 0 ;
    int hold ;
    char array[50]  ;
    int num ; 
    int    line ;
    const int maxchar = 7 ;
    ofstream File;
    
    int main ()
    {
        File.open("File.txt",ios::out);
        line = 0 ;
        num = 0 ;
        array[1] = '\0' ;
        fill ( ) ;
        File.close();
    
        return 0 ;
    
    }
    
    void fill (  ) 
    {
        array[num] = 'a' ;
        array[num +1] = '\0' ;
        for ( i = 0 ; i < 25 ; i++ )
        {
            File << array << " " ;
            array[num]++ ;    
        }
            File << array << " " ;
        testif ( ) ;
    }
    
    void testif ( ) 
    {
        if ( line == 0)
            addchar( ) ;
        if ( array [line -1 ] == 'z' ){
            line-- ;
            testif ( ) ;
        }
        else {
            array [line - 1 ] ++ ;
            hold = line ;
            refill () ;
            filla() ;
        }
            testif ( ) ;
    }
    
    void addchar () 
    {
        if ( line < maxchar ) 
        {
            num ++ ;
            line = num ;
            array [num +1] = '\0' ;
            for ( i = 0 ; i < num ; i++ )
                array[i] = 'a' ;
            filla( ) ;
        }
    
    }
    
    void filla ( ) 
    {
        array[num] = 'a' ;
        array[num +1] = '\0' ;
        for ( i = 0 ; i < 25 ; i++ )
        {
            File << array << " " ;
            array[num]++ ;    
        }
            File << array << " " ;
        testif ( ) ;
    }
    
    void refill()
    {
            for ( hold ; hold <= num ; hold++ ) {
                array[hold] = 'a' ; 
                }
            line  = num ;
            filla() ;
    }
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    When I compile the code in Visual Studio I get the following warning:
    warning C4717: 'testif' : recursive on all control paths, function will cause runtime stack overflow

    That could account for the crash...
     
  7. bold-ally

    bold-ally New Member

    Joined:
    Apr 25, 2009
    Messages:
    8
    Likes Received:
    1
    Trophy Points:
    0
    Could this be avoided somehow ??
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Only by redesigning testif. Why does it need to call itself? In particular why does the last thing it does need to be to call itself? If it should call itself sometimes and not other times, then maybe an if statement would remove the infinite recursion.

    It seems very complicated code for something that is essentially just a base 26 counter. If the program were to display all number combinations from 0000 to 9999, without using integers (i.e. still using the char array), how would you go about that? You'd want to increment the last digit if it's less than 9, but if it's 9 set it back to zero and increment the number to the left. And if the number to the left is 9, reset it to zero and increment the one to the left. And so on, until you're at the leftmost digit, and when that wraps round to zero you know you're done. Then when you've got that sorted, just tweak the code so that instead of counting each column from 0-9, count from a-z.
     
  9. bold-ally

    bold-ally New Member

    Joined:
    Apr 25, 2009
    Messages:
    8
    Likes Received:
    1
    Trophy Points:
    0
    Well i fixed it ^^
    It runs flawlessly now ^^
    Thank allot for the tip !

    Code:
    [FONT=monospace]
    [LIST=1]
    [*]#include <fstream.h>
    [*] 
    [*]void refill() ;
    [*]void fill ( ) ;
    [*]void testif ( ) ;
    [*]void addchar ( ) ;
    [*]void filla ( ) ;
    [*] 
    [*]int i = 0 ;
    [*]int hold ;
    [*]char array[50]  ;
    [*]int num ; 
    [*]int     line ;
    [*]const int maxchar = 6 ;
    [*]ofstream File;
    [*] 
    [*]int main ()
    [*]{
    [*]        File.open("File.txt",ios::out);
    [*]        line = 0 ;
    [*]        num = 0 ;
    [*]        array[1] = '\0' ;
    [*]        fill ( ) ;
    [*]        File.close();
    [*] 
    [*]        return 0 ;
    [*] 
    [*]}
    [*] 
    [*]void fill (  ) 
    [*]{
    [*]        array[num] = 'a' ;
    [*]        array[num +1] = '\0' ;
    [*]        for ( i = 0 ; i < 25 ; i++ )
    [*]        {
    [*]                File << array << " " ;
    [*]                array[num]++ ;  
    [*]        }
    [*]                File << array << " " ;
    [*]        testif ( ) ;
    [*]}
    [*] 
    [*]void testif ( ) 
    [*]{
    [*]        while ( num != maxchar ) {
    [*]        if ( line == 0)
    [*]                addchar( ) ;
    [*]                filla();
    [*]        if ( array [line -1 ] == 'z' ){
    [*]                line-- ;
    [*]        }
    [*]        else {
    [*]                array [line - 1 ] ++ ;
    [*]                hold = line ;
    [*]                refill () ;
    [*]                filla() ;
    [*]                }
    [*]        }
    [*]}
    [*] 
    [*]void addchar () 
    [*]{
    [*]                num ++ ;
    [*]                line = num ;
    [*]                array [num +1] = '\0' ;
    [*]                for ( i = 0 ; i < num ; i++ )
    [*]                        array[i] = 'a' ;
    [*] 
    [*]}
    [*] 
    [*]void filla ( ) 
    [*]{
    [*]        if ( num != maxchar ) {
    [*]        array[num] = 'a' ;
    [*]        array[num +1] = '\0' ;
    [*]        for ( i = 0 ; i < 25 ; i++ )
    [*]        {
    [*]                File << array << " " ;
    [*]                array[num]++ ;  
    [*]        }
    [*]                File << array << " " ;
    [*]}
    [*]}
    [*]void refill()
    [*]{
    [*]                for ( hold ; hold <= num ; hold++ ) {
    [*]                        array[hold] = 'a' ; 
    [*]                        }
    [*]                line  = num ;
    [*]}
    [/LIST]
    [/FONT]
    
    
     
  10. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well done Allot, whoever you are. You've certainly helped a lot.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice