Thread: using for loops to display a parallelogram

  1. #1
    Registered User helloalyssa's Avatar
    Join Date
    Sep 2010
    Posts
    25

    using for loops to display a parallelogram

    our output should look like this (the underscores are just there to make it display properly):

    This program will output a parallelogram.
    How long do you want each side to be? 9
    Please enter the character you want it to be made of: *
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    _********
    __*******
    ___******
    ____*****
    _____****
    ______***
    _______**
    ________*



    i'm almost done but i'm having a problem with getting the bottom half to indent properly. here is the code i have for it:

    Code:
    for (int row = length - 1; row >= 1; row--)
    
      {
    
        for (int col = 1; col <= row; col++)
    
        {
    
          cout << setw(1 + length - row) << displayChar;
    
        } 
    
        cout << "\n";
    
      }
    i tried putting in length and row for setw but they shift too far to the right. the equation that's in there now is something a tutor suggested to me but it's not working either. could anyone help me? thanks!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    One debugging technique is to try outputting the value of the equation to see if it's giving the number you expect. If it isn't, adjust the equation until it does, and then hopefully you will also get proper output. If it is giving you the number you expect, then maybe setw isn't working as you expect or there is some other problem.

  3. #3
    Registered User helloalyssa's Avatar
    Join Date
    Sep 2010
    Posts
    25
    i don't think there is another problem besides setw. it complies correctly and displays the right amount of characters but the spacing is off. it's definitely setw not working as i thought it would but after checking the forums for this problem for my online class, everyone seems to be encountering the same problem.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Is this how the parallelogram is supposed to look (with a length of 4):
    Code:
    *
    **
    ***
    ****
     ***
      **
       *
    ?

    If that's the case, then your code itself is the problem. I would start with the first part where you are outputting characters with no spaces before them (or between them). See if you can get that working first. You shouldn't need setw for that at all.

    Once you have that working, see if you can get the second half where there are spaces in front. Think about what setw does and when you're supposed to call it. A good trick is to just follow the code step by step with a pencil and paper and watch what it does (a debugger is actually better if you know how to use yours).

  5. #5
    Registered User helloalyssa's Avatar
    Join Date
    Sep 2010
    Posts
    25
    yeah it's supposed to look like that. the code i have posted is for the second half of the triangle. my teacher told us to make the code like that; code the first half then the second half.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ok, that's good. That makes sense. Now you just have to think about what setw is supposed to do, and what it does where you're calling it. Step through the loops with an example value and see what it is doing. Or add debugging code, something like this just before the line that calls it:
    Code:
    cout << "{calling setw with value: " << 1 + length - row << "}";
    Hopefully that will give you a clue as to what is going wrong here (it's actually just a small mistake).

  7. #7
    Registered User helloalyssa's Avatar
    Join Date
    Sep 2010
    Posts
    25
    thank you! i figured it out with the help of you and the local lab tutor. heh. all i had to was nest an if loop into the nested for loop.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Cool. I fixed it a different way (moving the setw outside of the inner loop), but if it works, great.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Program that creates a parallelogram?
    By matthayzon89 in forum C Programming
    Replies: 5
    Last Post: 05-09-2010, 03:42 PM
  3. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  4. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM