Thread: problem creating program to the specific specification

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    42

    problem creating program to the specific specification

    Hi all

    I was wondering if I could have a little help on this. I currently have a code which will make the " * " character print out a triangle pattern. It currently prints out in the format:

    Code:
    How many lines? (an example of 4)
    
                             *
                           *  *
                         *  *  *
                      *  *  *  *
    but I want it to be printed like this:

    Code:
    *
    * *
    * * *
    * * * *

    The code I am using is:

    Code:
    #include <iostream>
    using namespace std;
    
    void printStars(int blanks, int starsInLine);
    
    int main()
    {
      int noOfLines;
      int counter;
      int noOfBlanks;
    
      cout << "How many lines?";
      cin >> noOfLines;
    
      while (noOfLines < 0 || noOfLines > 4)
        {
          cout << "Number of star lines should be between 1 and 4" << endl ;
          cout << "How many lines?";
          cin >> noOfLines;
        }
    
      cout << endl << endl ;
      noOfBlanks = 30;
    
      for (counter = 1; counter <= noOfLines; counter++)
        {
          printStars(noOfBlanks, counter) ;
          noOfBlanks--;
        }
    
      return 0;
    
    }
    
    void printStars(int blanks, int starsInLine)
    {
      int count;
    
      for (count = 1; count <= blanks; count++)
        cout << ' ';
      for (count = 1; count <= starsInLine; count++)
        cout << " * ";
      cout << endl ;
    }
    any ideas on how to make it print out in the format of

    *
    * *
    * * *
    * * * *

    would be much appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Stop printing spaces as padding, perhaps?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Why do I get the feeling that you found this code on the web and have no idea how it works?

    I mean, there's no way you could have written this code and not know why it prints the stars centered, or how to make them left-aligned.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by CornedBee View Post
    Why do I get the feeling that you found this code on the web and have no idea how it works?

    I mean, there's no way you could have written this code and not know why it prints the stars centered, or how to make them left-aligned.
    Ok I got it from a book, but I would like to get it working so that it can print it out like

    *
    * *
    * * *
    * * * *

    Any help would be great.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by rushhour View Post
    Ok I got it from a book, but I would like to get it working so that it can print it out like

    *
    * *
    * * *
    * * * *

    Any help would be great.
    If I do that, will you make my computer architecture homework?
    (I'll bet your ass this is a homework question

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by EVOEx View Post
    If I do that, will you make my computer architecture homework?
    (I'll bet your ass this is a homework question
    But the thing is, I am stuck, and need advice from expert programmers. I joined this forum thinking I could get help. I find that I learn more when someone does it, and it helps me complete the other tasks set, if you get me. So please, just this once can you help me.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Describe what the current printStars() function does. If you understand what it does, you will understand how to change it to do what you want it to do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by laserlight View Post
    Describe what the current printStars() function does. If you understand what it does, you will understand how to change it to do what you want it to do.
    Thanks ive got it working now!

    But can you give me a hint on how to get it to this style:

    Code:
    *******
      *****
        ***
          *
    Thanks in advance.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    sorry i mean an upside equalateral triangle

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Well, why not start with your "mistake"? If you can generate this:
    Code:
    *******
      *****
        ***
          *
    you'll soon be able to generate the equilateral triangle.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Quote Originally Posted by laserlight View Post
    Well, why not start with your "mistake"? If you can generate this:

    you'll soon be able to generate the equilateral triangle.
    I am sorry but I really am struggling now. It is probably obvious to do, but right now I am really getting frustrated with it and need yoyr help. I need the triangle to look like:

    Code:
    *********
     *******
      *****
       ***
        *

    This will be the last thing I ask. And any help would be much appreciated.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Imagine that you are describing to someone over the phone how to draw such a triangle - then you can probably figure out how to do it on the computer too!.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    First write the code to print this:
    Code:
    *********
    *******
    *****
    ***
    *
    It is just a variant of what you have done, so it should be easy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    42
    Sorry still having problems.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  2. Replies: 4
    Last Post: 05-25-2008, 12:31 AM
  3. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM