Thread: For Loop question for printing characters

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

    For Loop question for printing characters

    I am trying to print this flag---to have all the three columns of colors print side by side with a given input. this is what i have so far:
    Code:
    flagWidth = flagSize * 3;
     lineColor = flagSize;                                                                
      for(lineColor = 0; lineColor < flagWidth; lineColor++){                              
          for(lineColor2 = 0; lineColor2 < flagWidth; lineColor2++){                         
            color = 'B';                                                                     
            printf("&#37;c", color);                                                             
          }                                                                                  
          printf("\n");                                                                      
          for(lineColor = 0; lineColor < flagWidth; lineColor++){                            
            for(lineColor2 = 0; lineColor2 < flagWidth; lineColor2++){                       
              color = 'R';                                                                   
              printf("%c", color);                                                           
            }                                                                                
            printf("\n");                                                                    
          }                   
      for(lineColor = 0; lineColor < flagWidth; lineColor++){                            
            for(lineColor2 = 0; lineColor2 < flagWidth; lineColor2++){                       
              color = 'G';                                                                   
              printf("%c",color);                                                            
            }                                                                                
            printf("\n");                                                                    
          }                                                                                  
        }                                                                                    
      }                                                                                      
      else

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    lineColor = flagSize;
    That statement is redundant. You assign a different value to lineColor in the very next line, in the initialization section of a for loop.

    Are you looking for something like this?
    Code:
    RRR GGG BBB
    RRR GGG BBB
    RRR GGG BBB
    while right now you have something like this?
    Code:
    RRR
    RRR
    RRR
    
    GGG
    GGG
    GGG
    
    BBB
    BBB
    BBB
    In that case, I'm going to quote myself:
    Remember that you have to print all of the data you want on a line before you print a newline. Once you print a newline, you can't modify that line at all any more.
    In other words, you have to print "RRR GGG BBB" at one time, before you go printing a newline.

    You've almost got it, but you're printing newlines by accident instead of spaces. Try replacing your first two
    Code:
    printf("\n");
    with
    Code:
    printf(" ");
    and see what happens.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    ok yea i understand..thank you...it worked out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  2. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM