Run-Time Check Failure #2 - Stack around the variable 'string' was corrupted.

This is a discussion on Run-Time Check Failure #2 - Stack around the variable 'string' was corrupted. within the C Programming forums, part of the General Programming Boards category; having this error appear everytime, the program executes what it is suppose to, any ideas? many thanks Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Run-Time Check Failure #2 - Stack around the variable 'string' was corrupted.

    having this error appear everytime, the program executes what it is suppose to, any ideas?

    many thanks

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
    	char string[3];
    int count;
    
    
    printf("Please Input 4 Capital Letters\n"); 
    scanf("%5s",string);
    		for(count=0;count<=3;++count)
    		switch (string[count])		
    		{
    	printf("\n");
    	
    	case 'A':	printf("  a  \n"); 
    				printf(" a a \n"); 
    				printf(" aaa \n"); 
    				printf(" a a \n"); 
    				printf("a   a\n");break;
    	
    	case 'B':	printf("bbbbb\n"); 
    				printf("b   b\n"); 
    				printf("bbbbb\n"); 
    				printf("b   b\n"); 
    				printf("bbbbb\n");break;
    	
    	case 'C':	printf(" cccc\n"); 
    				printf("c    \n"); 
    				printf("c    \n"); 
    				printf("c    \n"); 
    				printf(" cccc\n");break;
    	
    	case 'D':	printf("dddd \n"); 
    				printf("d   d\n"); 
    				printf("d   d\n"); 
    				printf("d   d\n"); 
    				printf("dddd \n");break;
    	
    	case 'E':	printf("eeeee\n"); 
    				printf("e    \n"); 
    				printf("eeeee\n"); 
    				printf("e    \n"); 
    				printf("eeeee\n");break;
    	
    	case 'F':	printf("fffff\n"); 
    				printf("f    \n"); 
    				printf("fffff\n"); 
    				printf("f    \n"); 
    				printf("f    \n");break;
    	
    	case 'G':	printf("ggggg\n"); 
    				printf("g    \n"); 
    				printf("g  gg\n"); 
    				printf("g   g\n"); 
    				printf("ggggg\n");break;
    	
    	case 'H':	printf("h   h\n"); 
    				printf("h   h\n"); 
    				printf("hhhhh\n"); 
    				printf("h   h\n"); 
    				printf("h   h\n");break;
    	
    	case 'I':	printf("iiiii\n"); 
    				printf("  i  \n"); 
    				printf("  i  \n"); 
    				printf("  i  \n"); 
    				printf("iiiii\n");break;
    	
    	case 'J':	printf("jjjjj\n"); 
    				printf("    j\n"); 
    				printf("    j\n"); 
    				printf("  j j\n"); 
    				printf("  jjj\n");break;
    	
    	case 'K':	printf("k   k\n"); 
    				printf("k  k \n"); 
    				printf("kkk  \n"); 
    				printf("k  k \n"); 
    				printf("k   k\n");break;
    	
    	case 'L':	printf("l    \n"); 
    				printf("l    \n");
    				printf("l    \n");
    				printf("l    \n"); 
    				printf("lllll\n");break;
    	
    	case 'M':	printf("m m m\n"); 
    				printf("mm mm\n"); 
    				printf("m m m\n");
    				printf("m   m\n"); 
    				printf("m   m\n");break;
    	
    	case 'N':	printf("n   n\n"); 
    				printf("nn  n\n"); 
    				printf("n n n\n"); 
    				printf("n n n\n"); 
    				printf("n  nn\n");break;
    	
    	case 'O':	printf("ooooo\n");
    				printf("o   o\n");
    				printf("o   o\n");
    				printf("o   o\n"); 
    				printf("ooooo\n");break;
    	
    	case 'P':	printf("ppppp\n");
    				printf("p   p\n");
    				printf("ppppp\n");
    				printf("p    \n");
    				printf("p    \n");break;
    	
    	case 'Q':	printf("qqqqq\n");
    				printf("q   q\n");
    				printf("qqqqq\n"); 
    				printf("   q \n"); 
    				printf("   qq\n");break;
    	
    	case 'R':	printf("rrrrr\n");
    				printf("r   r\n"); 
    				printf("rrrrr\n");
    				printf("r  r \n"); 
    				printf("r   r\n");break;
    	
    	case 'S':	printf("sssss\n");
    				printf("s    \n");
    				printf("sssss\n");
    				printf("    s\n"); 
    				printf("sssss\n");break;
    	
    	case 'T':	printf("ttttt\n");
    				printf("  t  \n");
    				printf("  t  \n");
    				printf("  t  \n"); 
    				printf("  t  \n");break;
    	
    	case 'U':	printf("u   u,\n");
    				printf("u   u\n"); 
    				printf("u   u\n"); 
    				printf("u   u\n");
    				printf("uuuuu\n");break;
    	
    	case 'V':	printf("v   v\n"); 
    				printf("v   v\n"); 
    				printf(" v v \n"); 
    				printf(" v v \n"); 
    				printf("  v  \n");break;
    	
    	case 'W':	printf("w   w\n"); 
    				printf("w   w\n"); 
    				printf("w w w\n"); 
    				printf("ww ww\n"); 
    				printf("wwwww\n");break;
    		
    	case 'X':	printf("x   x\n"); 
    				printf(" x x \n"); 
    				printf("  x  \n"); 
    				printf(" x x \n"); 
    				printf("x   x\n");break;
    	
    	case 'Y':	printf("y   y\n"); 
    				printf(" y y \n");
    				printf("  y  \n"); 
    				printf("  y  \n"); 
    				printf("  y  \n");break;
    	
    	case 'Z':	printf("zzzzz\n"); 
    				printf("   z \n");
    				printf("  z  \n"); 
    				printf(" z   \n"); 
    				printf("zzzzz\n");break;
    
    
    	default :printf("out of range, please enter a letter\n");
    	}
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    Stop making new threads for what is basically the same problem.

    You're getting all mixed up and not thinking through the advice given and what it means. Maybe it will help if we use a constant, so you can understand the relationship between the size used for array declaration, in scanf calls and in array manipulation:
    Code:
    #define SIZE 4
    int main()
    {
        char string[SIZE + 1];  // +1 for the null character
        int count;
    
    
        printf("Please Input %d Capital Letters\n", SIZE); 
        scanf("%4s",string);  // can't use SIZE here
        for(count = 0; count < SIZE; ++count)
            switch (string[count])

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    On line 7 you define string as ... char string[3];
    On line 11 you ask for 4 characters. (out of bounds)
    On line 12 you scanf() for 5 characters. (way out of bounds)
    On line 13 you start a loop from 0 to <=3 (also out of bounds)


    change line 7 to ... char string[5];
    change line 12 to ... scanf("%4s",string);
    change line 13 to ... for (count = 0; count < 5; count++)

    As already explained in your other thread(s) ... a string array needs one slot for each character plus an extra for the trailing null. Going outside the array's bounds is going to overwrite memory the array doesn't own and may (did) crash your program.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    my thanks to you for helping me with this problem , i guess im just a little worried/concerned why it isnt working, i have taken your comments onboard, and have adjusted the code accordingly. the error of the run failure #2 is fixed, now the program just auto escapes

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    What does "auto escapes" mean? Perhaps this is your problem: Cprogramming.com FAQ > Stop my Windows Console from disappearing everytime I run my program?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-05-2010, 10:34 AM
  2. Replies: 14
    Last Post: 11-17-2008, 11:31 AM
  3. stack around the variable corrupted
    By chintugavali in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2008, 12:01 PM
  4. Replies: 3
    Last Post: 05-22-2007, 11:42 PM
  5. Stack around variable corrupted?
    By timmygee in forum C++ Programming
    Replies: 7
    Last Post: 12-10-2005, 07:19 AM

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