Hollow square

This is a discussion on Hollow square within the C++ Programming forums, part of the General Programming Boards category; Okay, here's my hollow square code, but why is it inserting asterisks in odd spots? Code: #include <iostream> using namespace ...

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Hollow square

    Okay, here's my hollow square code, but why is it inserting asterisks in odd spots?

    Code:
    #include <iostream>
    
    using namespace std;
    
    // begins program execution
    int main()
    {
       int side;  // holds size of side of square
       int i,j;   // holds counters; i = row and j = column
       
       cout << "Input the size of a side of a square (1-20): "; // asks user for size of the side
       cin >> side; 
       
                  // for top row
       for ( i = 0; i < side; ++i)
        {
           cout << "*";
        }
     
                  // for middle rows subtract 2 to adjust for first and last row
        for ( i = 0; i < ( side - 2 ); ++i)
        {
           cout << "*";
                  // subtract 2 to adjust for first and last columns
           for ( j = 0; j < ( side - 2 ); ++j)
           {
              cout << " ";
           }
           cout << "*" << endl;
        }
    
    	          // for bottom row
             for ( i = 0; i < side; ++i)
    		 {
           cout << "*";
    		 }
        cout << endl;
    
    	return 0;
    
    } // end main function
    THE redheaded stepchild.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    What's the output? It looks good as far as I can see.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Oh yes, I ran it, and:
    Code:
                   // for top row
       for ( i = 0; i < side; ++i)
        {
           cout << "*";
        }
     
        cout << endl; // need a newline
    
                  // for middle rows subtract 2 to adjust for first and last row
        for ( i = 0; i < ( side - 2 ); ++i)
        {
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    It looks like you need to enter a new line or an endl after the first line of asterixes and before you start the second line.
    You're only born perfect.

  5. #5
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Too easy! Thanks much.
    THE redheaded stepchild.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hollow square
    By mdnealy in forum C# Programming
    Replies: 8
    Last Post: 07-05-2009, 03:23 PM
  2. Loop seg error
    By Zishaan in forum Game Programming
    Replies: 2
    Last Post: 03-28-2007, 01:27 PM
  3. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  4. Help with my draughts game
    By Zishaan in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 07:33 AM
  5. hollow square
    By jms318 in forum C Programming
    Replies: 5
    Last Post: 11-30-2006, 10:36 PM

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