hello friends
i start learning c++ and still at basics and was trying to solve a exercise in second chapter. this exercise asked to print a square using asterisks. i am able to create a asterisks solid square and colde is as follows.

# include<iostream>

using namespace std;

int main()
{
    int row;
    int col;
    int size;
    
   cout << "Please enter the size of square: ";
   cin >> size;

   for(col=1; col<=size; col++) 
   {    
       cout <<endl<<endl;
   
       for(row=1; row <=size; row++)
           {
                          cout << " * ";
           }
   }
    system("pause");
    return 0;
    
}

problem is that i dont need solid square only sides and program should be able to create any size of squrare specified by user. Desired output should look like this

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

Please can any one give me hint what should i do, i dont need code at the moment i wanna have a go at it first. thanx in advance for any tips.

Consider testing the current value of col and row, only display the * if row is top or bottom, or if col is left or right edge.

You also should consider getting in the habit of processing in a row oriented fashion. It may not make any difference in this problem, but in future work it will.

And, when you work with arrays, the usual loop is for( i = 0; i < size; i++ ) Another habit you should develop.

Thankyou vmanes for your advice i will try to keep in mind. taking your hint i did comeup with something which is as follows

# include<iostream>

using namespace std;

int main()
{
    int row;
    int col;
    int i;
    
    cout << "Please enter the size of square: ";
    cin >> i;
   for(col=1; col<=i; col++) 
   {    
       cout <<endl<<endl;
       
       for(row=1; row <=i; row++)
           {
             if((col==1 || col==i) || (row==1))            // || row ==i))  
             {                                                              
               cout << " * ";
             }
               
           }
   }
    system("pause");
    return 0;
    
}

i purposely made some part of "if" statement as coments it only shows that i was trying to use it..if i run this program i am able to get 3 sides of square. but having trouble to get forth side at right place. i am sure some thing is wrong with ' if ' statment and other thing is this right way to put it or if statement getting too confusing.

Here's what you have to do:

# include<iostream>

using namespace std;

int main()
{
    int i;
    
    cout << "Please enter the size of square: ";
    cin >> i;
   for(int col=0; col<i; col++) 
   {    
       cout <<endl<<endl;
       
       for(int row=0; row<i; row++)
           {
             if((col==0 || col==i-1) || (row==0) || (row ==i-1))  
             {                                                              
               cout << " * ";
             }
             else cout<<"   ";
               
           }
   }
    system("pause");
    return 0;
    
}

First, notice my for-loops (row and col, both start from 0, not 1)
It doesn't affect program, but it's the more "normal" way in c++ :)

Second, what was your problem:
you didn't have else condition. Then, when if failed, nothing happened.
But now if "if" fails, a blank space is typed.
HTH

thankx vmanes
i used the same code as u sugested but same problem its output is somthing like this

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

code seems ok logically but not sure wats wrong
thnx for your time and helping me out.

thnx vmanes and sci@phy for helping me out and i just figured out what was wrong wiht my output which i just mentioned above , now its working perfectly fine. thnx again

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.