C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-16-2005, 03:23 PM   #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.
Kayoss is offline   Reply With Quote
Old 09-16-2005, 03:31 PM   #2
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
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, etc.

New project: nort
dwks is offline   Reply With Quote
Old 09-16-2005, 03:35 PM   #3
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
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, etc.

New project: nort
dwks is offline   Reply With Quote
Old 09-16-2005, 03:53 PM   #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.
elad is offline   Reply With Quote
Old 09-16-2005, 04:49 PM   #5
Registered User
 
Kayoss's Avatar
 
Join Date: Sep 2005
Location: California
Posts: 53
Too easy! Thanks much.
__________________
THE redheaded stepchild.
Kayoss is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
hollow square mdnealy C# Programming 8 07-05-2009 03:23 PM
Loop seg error Zishaan Game Programming 2 03-28-2007 01:27 PM
Forced moves trouble!! Zishaan Game Programming 0 03-27-2007 06:57 PM
Help with my draughts game Zishaan C++ Programming 9 03-24-2007 07:33 AM
hollow square jms318 C Programming 5 11-30-2006 11:36 PM


All times are GMT -6. The time now is 07:30 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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