Thread: For/loop statement

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

    For/loop statement

    What I am trying to do is create a program which will look similar to this:

    Enter character: &
    &&&&&
    &&&&&
    &&&&&

    here is what I have so far and please help me as much as you can.

    Code:
    void pattern ()
    
    		{ 
    				char ch;
    			cout << "enter character";
    				cin >> ch;
    			for (int i=1; i>=5; ++i)
    				ch;
    			for (int j=5; j>=1; --j)
    			cout << ch;
    			cout << endl;
    			}
    Am I going along the right path at least? also, this is a somehwat example my professor gave me, am I supposed to be using the i's and j's? what do they mean?

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    i and j are just typical iterators. I actually stands for index, and i and j are both used as imiginary numbers (i in math, j in engineering)

    Your formatting isn't good (your ide should do that for you, but it appears to have not)

    Also, it is not usual to pre-increment loops and use the >= method. Normally you would post increment and use < or <=.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by stevedawg85
    What I am trying to do is create a program which will look similar to this:

    Enter character: &
    &&&&&
    &&&&&
    &&&&&

    here is what I have so far and please help me as much as you can.

    Code:
    void pattern ()
    
    		{ 
    				char ch;
    			cout << "enter character";
    				cin >> ch;
    			for (int i=1; i>=5; ++i)
    				ch;
    			for (int j=5; j>=1; --j)
    			cout << ch;
    			cout << endl;
    			}
    Am I going along the right path at least? also, this is a somehwat example my professor gave me, am I supposed to be using the i's and j's? what do they mean?
    You don't have to use 'i' or 'j' .. you could use 'x' or 'a' or 'HelloMyNameIsFrank' .... they are just names.
    In this instance, they're the names of "loop counters" (they keep track of how many times your loop has run - the ++i increments the loop counter every time)

    a quick note about the middle expression.. i>=5
    You tell the for loop that int i=1; - meaning that the first time the loop runs through, 'i' is equal to 1 ..
    Actually, the loop will never run, because you tell it to only run while the middle expression is true: i>=5
    (1 is greater than or equal to 5? - Nope!)

    also, what is that "ch;" supposed to do after your first "for" statement? at the moment, it's just sitting there creating an error... (Take it out)

    By the way, you probably want your endl inside your first 'for' loop. (at the moment, it is dangling after both loops have ended) To do this, you need to use some curly brackets to show what your for loop is... eg,
    Code:
    for(int x=0; x!=10; ++x)
    {
       // All code between these curly brackets is repeated 10 times
    }
    Last edited by Bench82; 03-02-2006 at 08:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. Can repeated characters be printed with a C statement?
    By Pete_2000 in forum C Programming
    Replies: 4
    Last Post: 05-05-2006, 07:47 PM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM