I am trying to draw a hollow square and I am having a problem with nesting while loops within while loops. Is this possible?
The quoted part of code is the trouble part.

#include <iostream>
using namespace std;
int main(void)
{ 
  int sides, t=2, h=0, b=0, s;
  cout<<"Size of square: \n"; 
  cin>> sides;
  s=sides-2;
  cout<< "\n" ;
  cout<< s <<endl;
system("PAUSE"); 

do {
    cout<< "*" ;
    {for (;s!= 0;--s)
        cout<< " " ;
    cout<< "*" ;}
    ++h;

}while (h<sides);

cout<< "\n" ;
cout<< "\n" ;
system("PAUSE");

  return 0;
}

as far as I can tell, you're only "drawing" the sides of the square, you didn't do the top or bottom yet...

In the sides code, the internal loop (for the spaces) is destructively decrementing your limit. (I'm referring to the s-- that limits your loop.) You should either reset s (to sides - 2) just before the loop starts every time, or use another variable to count up to s instead of destroying it.

As an alternative, you could build a string up to be the "* *" with as many spaces as you require and then just output the string the appropriate number of times.

thanks much!!

took your advice and it works!!
the problem was really s was being destroyed, don't know how I missed it before!!
Here is the complete thing.
The user is suppose to input the length of the square they want.

//Draw a square of "x" size.
#include <iostream>
using namespace std;
int main(void)
{ 
  int sides, t=2, h=1, b=0, s;
  cout<<"Size of square: \n"; 
  cin>> sides;
   s=sides-2;
  cout<< "\n";

while (t!=sides){
      cout<< "*";
      ++t;
}
while (h!=sides){
      cout<< "*";
      for (;s!= 0;--s){
        cout<< " ";
      }    
      s=sides-2;     
      cout<< "*";
      ++h;
      cout<< "\n";
}            
while (b!=sides){
      cout<< "*";
      ++b;  
}   
cout<< "\n" ;
cout<< "\n" ;
system("PAUSE");

  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.