Like Tree1Likes
  • 1 Post By stahta01

Need help, cant get program to quit a loop

This is a discussion on Need help, cant get program to quit a loop within the C Programming forums, part of the General Programming Boards category; here is my program, it does what its suppose to, display a banner of the letters a user inputs, but ...

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

    Need help, cant get program to quit a loop

    here is my program, it does what its suppose to, display a banner of the letters a user inputs, but it doesn't break the loop, and will keep printing out the statements, where have I gone wrong?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
    char string[4];
    int count=0;
    int space=32;
    char a='a';char b='b';char c='c';char d='d';char e='e';char f='f';char g='g';char h='h';char i='i';
    char j='j';char k='k';char l='l';char m='m';char n='n';char o='o';char p='p';char q='q';char r='r';
    char s='s';char t='t';char u='u';char v='v';char w='w';char x='x';char y='y';char z='z';
    
    
    printf("Please Input 4 Capital Letters\n"); 
    scanf("%s", &string[4]);
        
    do
        switch (string[4])    
        
        {
         for(count=0;count>=4;count++);
        case 'A':printf("%d,%d,%c,%d,%d,\n",space,space,a,space,space); printf("%c,%c,%c,%c,%c,\n",space,a,space,a,space); printf("%c,%c,%c,%c,%c,\n",space,a,a,a,space); printf("%c,%c,%c,%c,%c,\n",space,a,space,space,a,space); printf("%c,%c,%c,%c,%c,\n",a,space,space,space,space,a);break;
        case 'B':printf("%c,%c,%c,%c,%c,\n",b,b,b,b,b); printf("%c,%c,%c,%c,%c,\n",b,space,space,space,b); printf("%c,%c,%c,%c,%c,\n",b,b,b,b,b); printf("%c,%c,%c,%c,%c,\n",b,space,space,space,b); printf("%c,%c,%c,%c,%c,\n",b,b,b,b,b);break;
        case 'C':printf("%c,%c,%c,%c,%c,\n",space,c,c,c,c); printf("%c,%c,%c,%c,%c,\n",c,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",c,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",c,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",space,c,c,c,c);break;
        case 'D':printf("%c,%c,%c,%c,%c,\n",d,d,d,d,space); printf("%c,%c,%c,%c,%c,\n",d,space,space,space,d); printf("%c,%c,%c,%c,%c,\n",d,space,space,space,d); printf("%c,%c,%c,%c,%c,\n",d,space,space,space,d); printf("%c,%c,%c,%c,%c,\n",d,d,d,d,space);break;
        case 'E':printf("%c,%c,%c,%c,%c,\n",e,e,e,e,e); printf("%c,%c,%c,%c,%c,\n",e,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",e,e,e,e,e); printf("%c,%c,%c,%c,%c,\n",e,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",e,e,e,e,e);break;
        case 'F':printf("%c,%c,%c,%c,%c,\n",f,f,f,f,f); printf("%c,%c,%c,%c,%c,\n",f,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",f,f,f,f,f); printf("%c,%c,%c,%c,%c,\n",f,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",f,space,space,space,space);break;
        case 'G':printf("%c,%c,%c,%c,%c,\n",space,g,g,g,g); printf("%c,%c,%c,%c,%c,\n",g,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",g,space,space,g,g); printf("%c,%c,%c,%c,%c,\n",g,space,space,space,g); printf("%c,%c,%c,%c,%c,\n",space,g,space,space,g);break;
        case 'H':printf("%c,%c,%c,%c,%c,\n",h,space,space,space,h); printf("%c,%c,%c,%c,%c,\n",h,space,space,space,h); printf("%c,%c,%c,%c,%c,\n",h,h,h,h,h); printf("%c,%c,%c,%c,%c,\n",h,space,space,space,h); printf("%c,%c,%c,%c,%c,\n",h,space,space,space,h);break;
        case 'I':printf("%c,%c,%c,%c,%c,\n",i,i,i,i,i); printf("%c,%c,%c,%c,%c,\n",space,space,i,space,space); printf("%c,%c,%c,%c,%c,\n",space,space,i,space,space); printf("%c,%c,%c,%c,%c,\n",space,space,i,space,space); printf("%c,%c,%c,%c,%c,\n",i,i,i,i,i);break;
        case 'J':printf("%c,%c,%c,%c,%c,\n",j,j,j,j,j); printf("%c,%c,%c,%c,%c,\n",space,space,space,space,j); printf("%c,%c,%c,%c,%c,\n",space,space,space,space,j); printf("%c,%c,%c,%c,%c,\n",space,space,j,space,j); printf("%c,%c,%c,%c,%c,\n",space,space,j,j,j);break;
        case 'K':printf("%c,%c,%c,%c,%c,\n",k,space,space,space,k); printf("%c,%c,%c,%c,%c,\n",k,space,space,k,space); printf("%c,%c,%c,%c,%c,\n",k,k,space,space,space); printf("%c,%c,%c,%c,%c,\n",k,space,k,space,space); printf("%c,%c,%c,%c,%c,\n",k,space,space,space,k);break;
        case 'L':printf("%c,%c,%c,%c,%c,\n",l,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",l,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",l,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",l,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",l,l,l,l,l);break;
        case 'M':printf("%c,%c,%c,%c,%c,\n",m,space,m,space,m); printf("%c,%c,%c,%c,%c,\n",m,m,space,m,m); printf("%c,%c,%c,%c,%c,\n",m,space,m,space,space); printf("%c,%c,%c,%c,%c,\n",m,space,space,space,m); printf("%c,%c,%c,%c,%c,\n",m,space,space,space,m);break;
        case 'N':printf("%c,%c,%c,%c,%c,\n",n,space,space,space,n); printf("%c,%c,%c,%c,%c,\n",n,n,space,space,n); printf("%c,%c,%c,%c,%c,\n",n,space,n,space,n); printf("%c,%c,%c,%c,%c,\n",n,space,n,space,n); printf("%c,%c,%c,%c,%c,\n",n,space,space,n,n);break;
        case 'O':printf("%c,%c,%c,%c,%c,\n",o,o,o,o,o); printf("%c,%c,%c,%c,%c,\n",o,space,space,space,o); printf("%c,%c,%c,%c,%c,\n",o,space,space,space,o); printf("%c,%c,%c,%c,%c,\n",o,space,space,space,o); printf("%c,%c,%c,%c,%c,\n",o,o,o,o,o);break;
        case 'P':printf("%c,%c,%c,%c,%c,\n",p,p,p,p,p); printf("%c,%c,%c,%c,%c,\n",p,space,space,space,p); printf("%c,%c,%c,%c,%c,\n",p,p,p,p,p); printf("%c,%c,%c,%c,%c,\n",p,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",p,space,space,space,space);break;
        case 'Q':printf("%c,%c,%c,%c,%c,\n",space,q,q,q,space); printf("%c,%c,%c,%c,%c,\n",q,space,space,space,q); printf("%c,%c,%c,%c,%c,\n",space,q,q,q,space); printf("%c,%c,%c,%c,%c,\n",space,space,space,q,space); printf("%c,%c,%c,%c,%c,\n",space,space,space,q,q);break;
        case 'R':printf("%c,%c,%c,%c,%c,\n",r,r,r,r,r); printf("%c,%c,%c,%c,%c,\n",r,space,space,space,r); printf("%c,%c,%c,%c,%c,\n",r,r,r,r,r); printf("%c,%c,%c,%c,%c,\n",r,space,space,r,space); printf("%c,%c,%c,%c,%c,\n",r,space,space,space,r);break;
        case 'S':printf("%c,%c,%c,%c,%c,\n",s,s,s,s,s); printf("%c,%c,%c,%c,%c,\n",s,space,space,space,space); printf("%c,%c,%c,%c,%c,\n",s,s,s,s,s); printf("%c,%c,%c,%c,%c,\n",space,space,space,space,s); printf("%c,%c,%c,%c,%c,\n",s,s,s,s,s);break;
        case 'T':printf("%c,%c,%c,%c,%c,\n",t,t,t,t,t); printf("%c,%c,%c,%c,%c,\n",space,space,t,space,space); printf("%c,%c,%c,%c,%c,\n",space,space,t,space,space); printf("%c,%c,%c,%c,%c,\n",space,space,t,space,space); printf("%c,%c,%c,%c,%c,\n",space,space,t,space,space);break;
        case 'U':printf("%c,%c,%c,%c,%c,\n",u,space,space,space,u); printf("%c,%c,%c,%c,%c,\n",u,space,space,space,u); printf("%c,%c,%c,%c,%c,\n",u,space,space,space,u); printf("%c,%c,%c,%c,%c,\n",u,space,space,space,u); printf("%c,%c,%c,%c,%c,\n",u,u,u,u,u);break;
        case 'V':printf("%c,%c,%c,%c,%c,\n",v,space,space,space,v); printf("%c,%c,%c,%c,%c,\n",space,v,space,v,space); printf("%c,%c,%c,%c,%c,\n",space,v,space,v,space); printf("%c,%c,%c,%c,%c,\n",space,v,space,v,space); printf("%c,%c,%c,%c,%c,\n",space,space,v,space,space);break;
        case 'W':printf("%c,%c,%c,%c,%c,\n",w,space,w,space,w); printf("%c,%c,%c,%c,%c,\n",w,space,w,space,w); printf("%c,%c,%c,%c,%c,\n",w,space,w,space,w); printf("%c,%c,%c,%c,%c,\n",w,w,w,w,w); printf("%c,%c,%c,%c,%c,\n",w,w,w,w,w);break;
        case 'X':printf("%c,%c,%c,%c,%c,\n",x,space,space,space,x); printf("%c,%c,%c,%c,%c,\n",b,space,space,space,b); printf("%c,%c,%c,%c,%c,\n",space,space,x,space,space); printf("%c,%c,%c,%c,%c,\n",space,x,space,x,space); printf("%c,%c,%c,%c,%c,\n",x,space,space,space,x);break;
        case 'Y':printf("%c,%c,%c,%c,%c,\n",y,space,space,space,y); printf("%c,%c,%c,%c,%c,\n",space,y,space,y,space); printf("%c,%c,%c,%c,%c,\n",space,space,y,space,space); printf("%c,%c,%c,%c,%c,\n",space,space,y,space,space); printf("%c,%c,%c,%c,%c,\n",space,space,y,space,space);break;
        case 'Z':printf("%c,%c,%c,%c,%c,\n",z,z,z,z,z); printf("%c,%c,%c,%c,%c,\n",space,z,space,space,space); printf("%c,%c,%c,%c,%c,\n",space,space,z,space,space); printf("%c,%c,%c,%c,%c,\n",space,space,space,z,space); printf("%c,%c,%c,%c,%c,\n",z,z,z,z,z);break;
    
    
    default :printf("out of range, please enter a letter\n");
    
    
    }while (count<=4);
    return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Good lord man... learn how to properly indent and format your source code. Nobody's gonna weed through that.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think you want <= not >= here.

    Code:
    for(count=0;count>=4;count++);
    You probably want to enclose something in a block with the for(), otherwise it won't do anything even if you correct the condition. However, using a for() inside a switch block around the cases will not work, because break will affect it.

    And like CommonTater says, please learn to format correctly.
    Last edited by MK27; 11-10-2011 at 11:11 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,552
    Why the semi colon after the for loop.
    Code:
    for(count=0;count>=4;count++);
    Try adding break in default case. oops, that break only out of switch.
    I suggest removed the do/while loop and learning how to use the for loop outside of the switch statement.

    Tim S.
    Last edited by stahta01; 11-10-2011 at 11:30 AM.
    programmer1 likes this.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Code:
    do
        switch (string[4])    
        
        {
         for(count=0;count>=4;count++);
        case 'A':
    Duff would not approve of you misusing his device. Like Tater said, fix your formatting and indentation. Never put more than one statement on a line. Then, fix the ridiculous use of char a = 'a', char space = 32, etc. If you want to print "y, , , y", then just put that in the format string and ditch the %c business. Then, take a second, look at where that for loop is and what it does.

    EDIT: And string has 4 elements, indexes 0..3. string[4] does not exist, so you are in the realm of undefined behavior.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's another suggestion: come up with some kind of function to do the repetitive printf() stuff, and/or place the letters into a const char matrix. Eg, try this:

    Code:
    #include <stdio.h>
    
    #define LETTER_HEIGHT 4
    
    const char *LetterA[] = {
    	"  a  ",
    	" a a ",
    	" aaa ",
    	" a a "
    };
    
    int main(void) {
    	int i;
    	for (i=0; i < LETTER_HEIGHT; i++)
    		printf("%s\n", LetterA[i]);
    	return 0;
    }
    Kind of nicer to work with, isn't it? You could then place all the letters in a separate header file:

    Code:
    #include "myBannerAlphabet.h"
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Actually here's a variation on that which may be more useful:

    Code:
    #include <stdio.h>
    
    #define LETTER_HEIGHT 5
    
    const char *Alphabet[26][5] = {
    	{
    		"  a  ",
    		" a a ",
    		" aaa ",
    		" a a ",
    		" a a "
    	},
    	{
    		"bbbbb",
    		"b   b",
    		"bbbb ",
    		"b   b",
    		"bbbbb"
    	}
    };
    
    int main(void) {
    	int i;
    	for (i=0; i < LETTER_HEIGHT; i++)
    		printf("%s\n", Alphabet[0][i]);
    	for (i=0; i < LETTER_HEIGHT; i++)
    		printf("%s\n", Alphabet[1][i]);
    	return 0;
    }
    With this one, you could get rid of the switch completely by simply using toupper() on the input, then subtracting 65 (the ASCII value for 'A') from each input character to get the array reference from *Alphabet[][].

    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't quit my program
    By Elite in forum C Programming
    Replies: 2
    Last Post: 03-04-2005, 05:09 AM
  2. Please help - how to quit a program
    By Ashkan in forum C Programming
    Replies: 8
    Last Post: 08-23-2003, 12:16 AM
  3. quit the loop?
    By lchunr in forum C Programming
    Replies: 6
    Last Post: 03-29-2003, 09:17 AM
  4. Loop won't quit!
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 07-13-2002, 09:22 PM

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