problem creating program to the specific specification

This is a discussion on problem creating program to the specific specification within the C++ Programming forums, part of the General Programming Boards category; Hi all I was wondering if I could have a little help on this. I currently have a code which ...

  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
    21,589
    Stop printing spaces as padding, perhaps?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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
    21,589
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,589
    Well, why not start with your "mistake"? If you can generate this:
    Code:
    *******
      *****
        ***
          *
    you'll soon be able to generate the equilateral triangle.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,589
    First write the code to print this:
    Code:
    *********
    *******
    *****
    ***
    *
    It is just a variant of what you have done, so it should be easy.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,589
    What is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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, 11:39 AM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM

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