Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   This program keeps crashing before it is over (http://www.go4expert.com/forums/program-crashing-t17126/)

bold-ally 26Apr2009 01:11

This program keeps crashing before it is over
 
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() ;
    }
}


bold-ally 26Apr2009 01:14

Re: This program keeps crashing before it is over
 
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

xpi0t0s 26Apr2009 04:08

Re: This program keeps crashing before it is over
 
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.

bold-ally 26Apr2009 04:52

Re: This program keeps crashing before it is over
 
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 .

bold-ally 26Apr2009 05:10

Re: This program keeps crashing before it is over
 
^^
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() ;
}


xpi0t0s 27Apr2009 02:05

Re: This program keeps crashing before it is over
 
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...

bold-ally 27Apr2009 22:32

Re: This program keeps crashing before it is over
 
Could this be avoided somehow ??

xpi0t0s 28Apr2009 03:18

Re: This program keeps crashing before it is over
 
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.

bold-ally 29Apr2009 23:02

Re: This program keeps crashing before it is over
 
Well i fixed it ^^
It runs flawlessly now ^^
Thank allot for the tip !

Code:

  1. #include <fstream.h>

  2. void refill() ;

  3. void fill ( ) ;

  4. void testif ( ) ;

  5. void addchar ( ) ;

  6. void filla ( ) ;

  7. int i = 0 ;

  8. int hold ;

  9. char array[50]  ;

  10. int num ;

  11. int    line ;

  12. const int maxchar = 6 ;

  13. ofstream File;

  14. int main ()

  15. {

  16.         File.open("File.txt",ios::out);

  17.         line = 0 ;

  18.         num = 0 ;

  19.         array[1] = '\0' ;

  20.         fill ( ) ;

  21.         File.close();

  22.         return 0 ;

  23. }

  24. void fill (  )

  25. {

  26.         array[num] = 'a' ;

  27.         array[num +1] = '\0' ;

  28.         for ( i = 0 ; i < 25 ; i++ )

  29.         {

  30.                 File << array << " " ;

  31.                 array[num]++ ;

  32.         }

  33.                 File << array << " " ;

  34.         testif ( ) ;

  35. }

  36. void testif ( )

  37. {

  38.         while ( num != maxchar ) {

  39.         if ( line == 0)

  40.                 addchar( ) ;

  41.                 filla();

  42.         if ( array [line -1 ] == 'z' ){

  43.                 line-- ;

  44.         }

  45.         else {

  46.                 array [line - 1 ] ++ ;

  47.                 hold = line ;

  48.                 refill () ;

  49.                 filla() ;

  50.                 }

  51.         }

  52. }

  53. void addchar ()

  54. {

  55.                 num ++ ;

  56.                 line = num ;

  57.                 array [num +1] = '\0' ;

  58.                 for ( i = 0 ; i < num ; i++ )

  59.                         array[i] = 'a' ;

  60. }

  61. void filla ( )

  62. {

  63.         if ( num != maxchar ) {

  64.         array[num] = 'a' ;

  65.         array[num +1] = '\0' ;

  66.         for ( i = 0 ; i < 25 ; i++ )

  67.         {

  68.                 File << array << " " ;

  69.                 array[num]++ ;

  70.         }

  71.                 File << array << " " ;

  72. }

  73. }

  74. void refill()

  75. {

  76.                 for ( hold ; hold <= num ; hold++ ) {

  77.                         array[hold] = 'a' ;

  78.                         }

  79.                 line  = num ;

  80. }



xpi0t0s 30Apr2009 21:36

Re: This program keeps crashing before it is over
 
Well done Allot, whoever you are. You've certainly helped a lot.


All times are GMT +5.5. The time now is 10:13.