Thread: Update to a problem

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

    Update to a problem

    HI guys, i have been working on the problem i had of displaying a 4 letter banner from an input, i have cleaned up the code, but am still having trouble understanding what i need to do next to make it only display 4 characters, i am new to this so if someone could explain it to me in "simple" terms that would be most appreciated

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
    char string[4];
    int count;
    
    
    printf("Please Input 4 Capital Letters\n"); 
    scanf("%3s", &string);
    for(count=0;count<=4;++count)
        switch (string[4])        
    {
        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
    May 2009
    Posts
    4,183
    Hint there is a problem in this line

    Code:
    switch (string[4])
    Tim S.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    sting[5]
    doh!
    anything else wrong?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    for(count=0;count<=4;++count)
        switch (string[4])
    string was declared to have 4 characters. In C, array indexes start at 0, meaning you only have string[0], string[1], string[2] and string[3]. string[4] does not exist and makes your program exhibit undefined behavior, which is bad. You need to fix your for loop and switch statement like so:
    Code:
    for(count = 0; count < 4; ++count)
        switch(string[count])

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Thanks, i have done that, now the program exits as soon as i have entered the four characters, any ideas?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    scanf("%3s", &string);
    for(count=0;count<=4;++count)
        switch (string[4])
    1) Remove the ampersand & from the front of string in the scanf() call.

    2) Change the 3 to a 5 in the format to the scanf() call

    3) Change string[4] to the fourth element - string[3]:

    0, 1, 2, 3, 4 << 5th element, this will be an end of string marker char.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    HI, with those changes i get the, Run-Time Check Failure #2 - Stack around the variable 'string' was corrupted. any ideas?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Update Files
    By nuddy in forum C Programming
    Replies: 2
    Last Post: 04-09-2010, 03:43 PM
  2. OpenGL Color update problem
    By arifin in forum C++ Programming
    Replies: 0
    Last Post: 07-22-2008, 12:17 AM
  3. Window just won't update!
    By Mithoric in forum Windows Programming
    Replies: 5
    Last Post: 03-04-2004, 08:38 AM
  4. Update to my medai player problem
    By Korn1699 in forum Tech Board
    Replies: 0
    Last Post: 11-22-2003, 08:58 PM
  5. Update
    By Unimatrix_001 in forum Windows Programming
    Replies: 1
    Last Post: 06-25-2003, 05:42 PM