Loop crashes

This is a discussion on Loop crashes within the C Programming forums, part of the General Programming Boards category; I haven't programmed since college so goes easy on me I have created a program which runs a loop inside ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    Loop crashes

    I haven't programmed since college so goes easy on me

    I have created a program which runs a loop inside a loop inside a loop (cat in the hat style) which will print a line of text to a text file not the screen and updates a character by 1 each time at the moment it only does 3 characters e.g

    aaa
    baa
    caa

    etc but crashes at zzk and just start to produce random ascii characters

    I'm doing this to brute force an in office puzzle were working on today.

    Can any one explain to me why it crashes at line (attempt) 7436 each time?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, your code is broken. [That's all I can say without actually seeing the code itself].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Here you go chap

    Code:
    #include <stdio.h>
    
    void main()
    {
        int a;
        int b;
        int c;
        int d;
        int e;
        int f;
        int g;
        int h;
        int n;
        
        char k1[25];
        char k2[25];
        char k3[25];
        char k4[25];
        char k5[25];
        char k6[25];
        char k7[25];
        char k8[25];
    
        FILE *fp;
                               
    k1[0]="a";
    k1[1]="b";
    k1[2]="c";
    k1[3]="d";
    k1[4]="e";
    k1[5]="f";
    k1[6]="g";
    k1[7]="h";
    k1[8]="i";
    k1[9]="j";
    k1[10]="k";
    k1[11]="l";
    k1[12]="m";
    k1[13]="n";
    k1[14]="o";
    k1[15]="p";
    k1[16]="q";
    k1[17]="r";
    k1[18]="s";
    k1[19]="t";
    k1[20]="u";
    k1[21]="v";
    k1[22]="w";
    k1[23]="x";
    k1[24]="y";
    k1[25]="z";
    
    k2[0]="a";
    k2[1]="b";
    k2[2]="c";
    k2[3]="d";
    k2[4]="e";
    k2[5]="f";
    k2[6]="g";
    k2[7]="h";
    k2[8]="i";
    k2[9]="j";
    k2[10]="k";
    k2[11]="l";
    k2[12]="m";
    k2[13]="n";
    k2[14]="o";
    k2[15]="p";
    k2[16]="q";
    k2[17]="r";
    k2[18]="s";
    k2[19]="t";
    k2[20]="u";
    k2[21]="v";
    k2[22]="w";
    k2[23]="x";
    k2[24]="y";
    k2[25]="z";
    
    k3[0]="a";
    k3[1]="b";
    k3[2]="c";
    k3[3]="d";
    k3[4]="e";
    k3[5]="f";
    k3[6]="g";
    k3[7]="h";
    k3[8]="i";
    k3[9]="j";
    k3[10]="k";
    k3[11]="l";
    k3[12]="m";
    k3[13]="n";
    k3[14]="o";
    k3[15]="p";
    k3[16]="q";
    k3[17]="r";
    k3[18]="s";
    k3[19]="t";
    k3[20]="u";
    k3[21]="v";
    k3[22]="w";
    k3[23]="x";
    k3[24]="y";
    k3[25]="z";
    
    k4[0]="a";
    k4[1]="b";
    k4[2]="c";
    k4[3]="d";
    k4[4]="e";
    k4[5]="f";
    k4[6]="g";
    k4[7]="h";
    k4[8]="i";
    k4[9]="j";
    k4[10]="k";
    k4[11]="l";
    k4[12]="m";
    k4[13]="n";
    k4[14]="o";
    k4[15]="p";
    k4[16]="q";
    k4[17]="r";
    k4[18]="s";
    k4[19]="t";
    k4[20]="u";
    k4[21]="v";
    k4[22]="w";
    k4[23]="x";
    k4[24]="y";
    k4[25]="z";
    
    k5[0]="a";
    k5[1]="b";
    k5[2]="c";
    k5[3]="d";
    k5[4]="e";
    k5[5]="f";
    k5[6]="g";
    k5[7]="h";
    k5[8]="i";
    k5[9]="j";
    k5[10]="k";
    k5[11]="l";
    k5[12]="m";
    k5[13]="n";
    k5[14]="o";
    k5[15]="p";
    k5[16]="q";
    k5[17]="r";
    k5[18]="s";
    k5[19]="t";
    k5[20]="u";
    k5[21]="v";
    k5[22]="w";
    k5[23]="x";
    k5[24]="y";
    k5[25]="z";
    
    k6[0]="a";
    k6[1]="b";
    k6[2]="c";
    k6[3]="d";
    k6[4]="e";
    k6[5]="f";
    k6[6]="g";
    k6[7]="h";
    k6[8]="i";
    k6[9]="j";
    k6[10]="k";
    k6[11]="l";
    k6[12]="m";
    k6[13]="n";
    k6[14]="o";
    k6[15]="p";
    k6[16]="q";
    k6[17]="r";
    k6[18]="s";
    k6[19]="t";
    k6[20]="u";
    k6[21]="v";
    k6[22]="w";
    k6[23]="x";
    k6[24]="y";
    k6[25]="z";
    
    k7[0]="a";
    k7[1]="b";
    k7[2]="c";
    k7[3]="d";
    k7[4]="e";
    k7[5]="f";
    k7[6]="g";
    k7[7]="h";
    k7[8]="i";
    k7[9]="j";
    k7[10]="k";
    k7[11]="l";
    k7[12]="m";
    k7[13]="n";
    k7[14]="o";
    k7[15]="p";
    k7[16]="q";
    k7[17]="r";
    k7[18]="s";
    k7[19]="t";
    k7[20]="u";
    k7[21]="v";
    k7[22]="w";
    k7[23]="x";
    k7[24]="y";
    k7[25]="z";
    
    k8[0]="a";
    k8[1]="b";
    k8[2]="c";
    k8[3]="d";
    k8[4]="e";
    k8[5]="f";
    k8[6]="g";
    k8[7]="h";
    k8[8]="i";
    k8[9]="j";
    k8[10]="k";
    k8[11]="l";
    k8[12]="m";
    k8[13]="n";
    k8[14]="o";
    k8[15]="p";
    k8[16]="q";
    k8[17]="r";
    k8[18]="s";
    k8[19]="t";
    k8[20]="u";
    k8[21]="v";
    k8[22]="w";
    k8[23]="x";
    k8[24]="y";
    k8[25]="z";
    
    
       fp = fopen("test.txt","a");
    
    
    
            
    //    printf("\nHello World\n");
    a=0;
    b=0;
    c=0;
    d=0;
    e=0;
    f=0;
    g=0;
    h=0;
    n=1;
    
    while (f < 26)
    {
    g=0;   
        while(g < 26)
        {
        	h=0;
        	while(h < 26)
        	{
    		
    		fprintf(fp,k1[h]);
    		fprintf(fp,k2[g]);
    		fprintf(fp,k3[f]);
    		fprintf(fp,"\n");
    //    		printf(k1[h]);
    //	    	printf(k2[g]);
    //	    	printf(k3[f]);
    //	    	printf(k4[e]);
    //	    	printf(k5[d]);
    //	    	printf(k6[c]);
    //	    	printf(k5[d]);
    //	    	printf(k7[b]);
    //	    	printf(k5[d]);
    //	    	printf(k8[a]);
    //	    	printf(k3[f]);
    //	    	printf(k1[h]);
    //	    	printf(k7[b]);	    	
    //    		printf("\n");
    	    	h++;
    	    	n++;
        	}
        g++;
        }  
    f++;
    }
    fclose(fp);    
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you want your arrays to actually be 26 long, rather than 25? There are, after all, 26 letters in the english alphabet.

    By the way, I'm 100% + sure that this can be done using a few nested for-loops instead of generating all the letters of the alphabet into arrays. [Or, if you must use an array, why not use just one, repeatedly]. And I would recommend "for" instead of "while" in this case - it makes it much easier to read the code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    I will alter the code for the loops to use FOR instead of WHILE

    As for the array of 26 I know its been a while since I did some programming but do you still count 0 as a number in arrays i.e 0-25 is 26 numbers??

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mrtilley View Post
    I will alter the code for the loops to use FOR instead of WHILE

    As for the array of 26 I know its been a while since I did some programming but do you still count 0 as a number in arrays i.e 0-25 is 26 numbers??
    Yes, but char array[10] is what you need to make an array of 10 elements. The elements are numbered 0..9, but you need 10 of them. Likewise, you need an index of 26 to make an array of 26 letters. [And you really only need one array at most - may even be better to generate on the fly using 'a'+x, where x is your loop counter 0..25. ]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    I was being a pleb and I dig what your saying now, I don't need separate arrays I only need one array and integer as counters

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    Besides which, initializing your arrays with null terminated character strings isn't what you want either.

    blah_array[0] = 'a';

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    rdrast: Good point - didn't even notice that one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

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