I have been giving the task of making a box using all asterisks, given an input of say 5. Im supposed to make something that resembles a 5x5 box with nothing in between...

I am stuck at a dead-end here and any help would be appreciated!

My code so far is below, it isn't right :P

``````#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
int clm;
int row;

cout<<"How big of square do you want? Enter a number 1-20: ";
cin>>clm;
while (clm-- > 0)
{
row = clm;
cout<<"*";

while (row++ > 0)
cout<<"*";
cout<<endl;
}

return 0;
}``````
``````while (row++ > 0)
cout<<"*";``````

If you ever get into this while-loop, will you ever get out of it?

Alright, you're on the right track, but you're creating an infinite loop. A better way to handle this would be with for loops instead of while.

What you should do is draw out a few examples and try to analyze what the output should look like. A 3x3 and 4x4 square should be something like below, with an o showing the open space.

``````***
*o*
***

****
*oo*
*oo*
****``````

So, following that outline, what should a code of NxN produce? Looks like the first line should be of N number of *. The next inner lines should be of 2 *'s with N-2 spaces in between, but make sure you consider the number of lines in the middle. Then finally the last line is a repeat of the first line it seems. Now, that's right, but what happens if you have a 1x1? Make sure you consider that before assuming you're done.

Thanks for the help! Unfortunately our instructor wants us to use "only what we have learned" so Im stuck with the while, else if, if statements :\

Seemed to have figured it out, although I think it could be greaty simplified somehow...but I'm not sure.

``````#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int  square, vertCount=0, horzCount=0;
cout<<"Enter square size: ";
cin>>square;

while (square > vertCount++)  {

if  ( vertCount==1 ) {

while ((square > horzCount++))

{
cout<<"*";
}

cout <<endl;
horzCount = 0;

}

else if (vertCount == square)

{

while (square > horzCount++ )

{
cout<<"*";
}

cout <<endl;
horzCount = 0;
}

else

{
cout<<"*";
while (square - 2 > horzCount++) {
cout<<" ";
}

cout<<"*";
cout<<endl;
horzCount = 0;
}
}
return 0;``````

Here is what I wrote,

``````#include<iostream>

using namespace std;

int main(int argc,char**argv){

int siz=0,i=0,n;
cout<< "\nHow big? ";
cin>> siz;
cout<< endl;

while(i<siz){
if(i==0||i==siz-1){
n=0;
while(n<siz){
cout<< "* ";
n++;
}
} else {
cout<< "* ";
n=2;
while(n<siz){
cout<< "  ";
n++;
}
cout<< "*";
}
cout<< endl;
i++;
}

return 0;

}``````
``````C:\c>gpp box.cpp -o box.exe
box.cpp:34:2: warning: no newline at end of file

C:\c>box

How big? [b]7[/b]

* * * * * * *
*           *
*           *
*           *
*           *
*           *
* * * * * * *

C:\c>``````
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.