I am very new to C++ need to draw rectangle "JUST THE BORDER" using for loops, the below code draws three sides but i am not able to draw the right side line please help if anyone can.

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{                               
   const int NUM_ACROSS = 6; 
   const int NUM_DOWN = 8;  
   int row; 
   int column;  
{
   for(row = 0; row < NUM_ACROSS; row++)           
      cout << "* ";

        for (column = 0; column < NUM_DOWN; column++)  
                  cout << "\n*";
              for (row = 5; row < NUM_ACROSS; row++)
                   cout << "\n";
                     for(row = 0; row < NUM_ACROSS; row++)  
                             cout << "* ";
}    

      cout << endl;
   system("PAUSE");
   return EXIT_SUCCESS;
}

u have to draw the left side line and right side line in the same loop by printing col number of spaces in between

1)Draw the top border
2)Draw the first '*' and follow it up with space
3)Repeat 2 until end
4) Draw the bottom border

Maybe this will help :

void drawB()
{
     const int C = 4;
     const int R = 4;

      for(int i = 0; i < R; i++)
     {
          for(int j = 0; j < C; j++)
           {
               cout<<"*";
            }
            cout<endl;
      }
    
      
}

So you know the code above draws :
* * * *
* * * *
* * * *

what you need inside the second for loop is a if/else statement.
But what will be the condition ?
Well from the picture we can see that Row 1 and Row 4 needs all the star, i.e i = 0 && i = 3 and in the middle we see that when j = 0 we need
a star, and the we need spaces until j = 3.
putting all these together we get something like this :

//inside the second for loop
  if( i == 0 || i == 3 ) /*print stars */
  else if(j == 0 || j == 3 ) /* print start */
  else /*print spaces */

So here is what the result might look like in java :

for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                if( i == 0 || i == 3)
                    System.out.print("*");
                else if(j == 0 || j == 3)
                    System.out.print("*");
                else System.out.print(" ");
            }
            System.out.println();
        }

u have to draw the left side line and right side line in the same loop by printing col number of spaces in between

How can i do that can you that will be appreciated if you can explain me or give me an example

Thanks,
Maverick

This is what I've been harping on since the major change-over at Daniweb. Java code is now flagged as C++ code instead of plain-old code if you don't specify Java-specific code tags. It automatically assumes the C++ language. It really bugs the hell out of me. I don't know about anyone else. Here's my thread on the topic.

http://www.daniweb.com/forums/thread220913.html

Sorry for the semi-thread-hijack, but it seemed relevant. firstPerson, you can specify Java-specific tags on any forum that will override the assumption, in case you didn't know:

for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                if( i == 0 || i == 3)
                    System.out.print("*");
                else if(j == 0 || j == 3)
                    System.out.print("*");
                else System.out.print(" ");
            }
            System.out.println();
        }

How can i do that can you that will be appreciated if you can explain me or give me an example

Thanks,
Maverick

Read firstPerson's post. He gives you an explanation and examples on how to do both a hollow and non-hollow rectangle. Change the System.out.print and System.out.println statements to the appropriate cout statements and what works in Java will work in C++. If you don't understand, please ask a more specific question.

u have to draw the left side line and right side line in the same loop by printing col number of spaces in between

Thanks a lot got it...

Maverick

hey guys lsn i hav a problem with drawing shapes with pointers..so can u explain to me how that works this is wat i hav so far..the thing is i need to complete the implementation files....

/* shapes program */

#include <iostream>
#include <cmath>
#include <windows.h>
#include <process.h>

using namespace std;

const double pi = 4*atan(1.0);

struct point
{
    double xCoordinate, yCoordinate;
};

class shape
{
public:
    virtual void draw(int) const = 0;
};

class square: public shape
{
public:
    void draw(int) const;
};

class rectangle: public shape
{
public:
    void draw(int) const;
};

class circle: public shape
{
public:
    void draw(int) const;
};

class star: public shape
{
public:
    void draw(int) const;
};

void delay();
void gotoxy(double, double);
void clrscr();
void line(point, point);
void circleShape(point, double);

void main()
{
    int step;
    shape *ptrArray[4];

    ptrArray[0] = new square;
    ptrArray[1] = new rectangle;
    ptrArray[2] = new circle;
    ptrArray[3] = new star;

    cout << "I advise you to extend the length of the output screen" << endl;
    cout << "type an integer value from 3 to 10 as the step value: ";
    cin >> step;

    for(int i=0; i<4; i++)
    {
        clrscr();
        ptrArray[i]->draw(step);
        delay();
        delay();
    }

  }

void square::draw(int step) const
{
    point center, p, q;
    double xCenter=40, yCenter=30;
    double hLength=5, vLength=5;
    int i;

    center.xCoordinate = xCenter;
    center.yCoordinate = yCenter;

    for(i=0; i<5; i++)
    {
        hLength += step/2;
        vLength += step/2;
        p.xCoordinate = center.xCoordinate - hLength/2;
        p.yCoordinate = center.yCoordinate - vLength/2;
        q.xCoordinate = center.xCoordinate + hLength/2;
        q.yCoordinate = center.yCoordinate - vLength/2;
        line(p, q);

        p = q;
        q.yCoordinate = q.yCoordinate + vLength;
        line(p, q);

        p = q;
        q.xCoordinate = p.xCoordinate - hLength;
        line(p, q);

        p = q;
        q.yCoordinate = p.yCoordinate - vLength;
        line(p, q);
        delay();
    }

    for(i=0; i<10; i++)
        cout << "\n";
}

void rectangle::draw(int step) const
{
        point center, l, w;
    double xCenter=40, yCenter=30;
    double hLength=5, vLength=5;
    int i;

    center.xCoordinate = xCenter;
    center.yCoordinate = yCenter;

    for(i=0; i<5; i++)
    {
        hLength += step/2;
        vLength += step/2;
        l.xCoordinate = center.xCoordinate - hLength/2;
        l.yCoordinate = center.yCoordinate - vLength/2;
        w.xCoordinate = center.xCoordinate + hLength/2;
        w.yCoordinate = center.yCoordinate - vLength/2;
        line(p, q);

        l = w;
        w.yCoordinate = w.yCoordinate + vLength;
        line(p, q);

        l = w;
        w.xCoordinate = l.xCoordinate - hLength;
        line(p, q);

        l = w;
        q.yCoordinate = p.yCoordinate - vLength;
        line(p, q);
        delay();
    }

    for(i=0; i<10; i++)
        cout << "\n";





}

void circle::draw(int step) const
{




}

void star::draw(int step) const
{




}

void line(point first, point second)
{
    int i, n=10;
    double hx, hy, xPos, yPos, cXc, cYc, slope=1;

    xPos = cXc = first.xCoordinate;
    yPos = cYc = first.yCoordinate;

    gotoxy(cXc, cYc);
    cout << '.';

    hx = second.xCoordinate - cXc;
    hy = second.yCoordinate - cYc;

    if(hx == 0)
        hy = hy/n;
    else
    {
        slope = hy/hx;
        hx /= n;
    }

    for(i=0; i<n-1; i++)
    {
        xPos += hx;
        if(hx == 0)
            yPos += hy;
        else
            yPos += slope*hx;
        gotoxy(xPos, yPos);
        cout << '.';
    }
}

void circleShape(point center, double radius)
{
    int i, n=10;
    double hx, hy, xPos, yPos, cXc, cYc, diff;

    hy = 2*radius/n;

    cXc = center.xCoordinate;
    cYc = center.yCoordinate;
    xPos = cXc;
    yPos = cYc-radius;

    gotoxy(cXc, yPos);
    cout << '.';

    for(i=0; i<n-1; i++)
    {
        yPos += hy;
        diff = yPos-cYc;
        hx = sqrt(radius*radius - diff*diff);
        xPos = cXc - hx;
        gotoxy(xPos, yPos);
        cout << '.';
        xPos = cXc + hx;
        gotoxy(xPos, yPos);
        cout << '.';
    }

    yPos += hy;
    gotoxy(cXc, yPos);
    cout << '.';
}

void delay( void )
{
    for (int i=0; i<100000000; i++)
         ;
}

void gotoxy(double x, double y)
{
    HANDLE hConsoleOutput;
    COORD dwCursorPosition;
    cout.flush();
    dwCursorPosition.X = x;
    dwCursorPosition.Y = y;
    hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
}

void clrscr()
{
    system("cls");
}
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.