Making a pattern using void()

This is a discussion on Making a pattern using void() within the C++ Programming forums, part of the General Programming Boards category; I am taking intro to c++ so this should be fairly simple for you c++ gurus. Here is an example ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    36

    Making a pattern using void()

    I am taking intro to c++ so this should be fairly simple for you c++ gurus. Here is an example of a program my professor wants me to write:

    Enter number of lines: 4
    Enter character: *

    ___*
    __**
    _***
    ****
    (^ignore the underscores, the only way i know how to display it^)

    This is what I have so far but I know i am wrong. Our professor gave us sort of a mini example so I tried my best to copy what he did but use what we are supposed to do, but i tried.
    Code:
    {
    pattern void (pattern)
    char ch;
    int lines;
    cout << "enter # of lines"
    cin >>lines;
    cout<< "Enter character"
    cin >> ch;
    for (intj=5; j>=5; ++j)
    ch;
    for (inti=lines; i<=lines; --lines)
    }
    Like I said, I KNOW i'm wrong and dont even know what the i, and j stand for :P. Point me in the right direction of more info and please help me as much as possible

    Thanks in advance,
    steve

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Did you learn the <iomanip> library yet?
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Like I said, I KNOW i'm wrong and dont even know what the i, and j stand for
    Did you learn the <iomanip> library yet?
    Whoa. Back up. Start with learning what a for-loop is.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    This is exactly one of the questions I had in my intro programming course right after we learned <iomanip>. That's why I'm asking. Even looking at the format of how the output looks in his example... doesn't it seem strikingly clear as to what simple function would cause that?
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    Learn it? ummm, kindof. He just says to put it at the top as one of the header files along with #include <iostream>. Thats pretty much it. and OH!, i know we have to use setw(). so yah......

    And with the for loop, we just learned that like last week, so I know the basics of a for loop.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Well there you go. Think setw() for a bit and it will come to you. Also, I may or may not have used a character array, as well. I can't remember.
    Last edited by SlyMaelstrom; 03-02-2006 at 02:57 PM.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    hmm, ok well......... so the way I have it is right? All i need to include is a setw()? Also, I DO use a for loop nested statement right. and with the setw(), would this go in one of the for statements or somewhere else?

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    You gotta think about it. Trust me, think about how setw() works and it will come to you.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    Ok, thanks for your help, but the main thing that concerns me is creating the actual pattern. You dont have to create it for me obviously, but can you write me a little program which will do basically what I am asking except for the design. just a simple program like this:

    enter # of lines: 3
    enter character: !
    !!!!!
    !!!!!
    !!!!!

    If not, I understand, but where can I get more help with patterns and similar programs like the one just described.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Writing the example you just gave there would put you one line away from writing the pattern in your original post. Let me say that you have some obvious errors in your original code that as 7stud suggested you might want to address before trying to do this assignment. Secondly, if your teacher didn't ask you to use <iomanip>, which I simply assumed because it was an assignment I got when I was at your level, then this program is just as easy to do without it.
    Sent from my iPadŽ

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    I think this is just an exercise in writing for loops, no setw() or anything else involved

    A for loop to count the number of lines, containing
    - a for loop to print spaces
    - a for loop to print stars

    And that's it.
    Just an exercise in counting, and using one for loop to control another.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    What I am getting confused about is how do I declare spaces to be in the first loop and the character to be in the second loop?

    I'm new to this whole for loop thing, hell, I'm new to programming in general

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by stevedawg85
    I'm new to this whole for loop thing, hell, I'm new to programming in general
    Here is the thing... and I don't mean to jump on your back here. Saying you're new doesn't mean much because this isn't a difficult program. Infact, it's an assignment that your teacher gave you, so someone that knows what you're supposed to know is saying it's not a difficult program. So by us telling you anymore really says little for the amount that you've learned in class.

    Also, don't say your teacher didn't go over loops well, because everyone uses that excuse and it's just flat out not true.
    Sent from my iPadŽ

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    What I am getting confused about is how do I declare spaces to be in the first loop
    cout<<" ";

    and the character to be in the second loop?
    cout<<ch;

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    Ok, i've been workin on this quite some time. I am literally standing at the doorway waiting for door to be unlocked. I almost have it but am missing something. Here is what I have:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void pattern ()
    
    { 
    char ch;
    int column;
    cout << "enter character";
    cin >> ch;
    cout << "enter # of lines";
    cin >> column;
              for (int i=column; i> 1; --i)
    	{
    	cout << " " << setw(i-1); 
              for (int j=1; j<column; ++j)
    	cout << ch;
    	cout << endl;
    	}
    }
    			
    
    int main ()
    {
            pattern ();
            return 0;
    }
    This program gives me the following output:
    enter lines: 5
    enter character: *
    ____****
    ___****
    __****
    _****
    (^^ignore the _'s again^^)
    Can someone point me in the direction of where to look at to see whats causing this problem. I believe I'm correct, but should the for statements go the other way around? Are the increments I'm doing right? Please help as much as you are able to

    thanks
    -steve

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Case Statement
    By danlee58 in forum C Programming
    Replies: 16
    Last Post: 11-23-2008, 08:46 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 05:24 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM

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