Hi I'm new to C++ and to here.

I have an assignment.

Make a HOLLOW rectangle using Functions.

Have the user imput the amount of rows and columns....and border width .

It should look something like this (It's supposed to be a HOLLOW rectangle) :

Enter number of rows and columns:
Enter Border width:


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

I'm only able to get this:

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

here's the part of my code that I know is affecting it :

//This function displays a square made of astericks

void displayStars (int rows, int cols)
{

for (int across = 0; across < rows; across++)
    {
         for (int down = 0; down < cols; down++)
         cout << "*";
         cout <<endl;
}
}

Recommended Answers

All 4 Replies

This should work (I think. I have been really ditzy when it comes to writing code lately)

for (int across = 0; across < rows; across++)
{
     if (across == 0 || accross == rows)
     {
          for (int down = 0; down < cols; down++)
               cout << "*";
          cout << endl;
     }
     else
     {
          for (int down = 0; down < cols; down++)
          {
               if (down == 0 || down == cols)
                    cout << "*";
               else
                    cout << " ";
          }
          cout <<endl;
     }
}

Edit: Whoops forgot about the top and bottom. As I said I am ditzy.

It didn't work. Oh no, i have another exam to study for tonight.

I've been sick and I'm so far behind.

This is my entire program:

#include <iostream>
using namespace std;


void displayStars (int, int);

int main ()
{ 
    int row, column;

    cout << "Enter the number of rows and columns: ";
    cin >> row >> column;

    while (column < 2 || column > 60 && row < 2 || row > 60)
    {
    cout << "Invalid values for rows and columns" <<endl;
    cout << "Please enter values between 2 - 60: " ;
    cin >> row >>column;
    }

    displayStars(row, column);      
    return 0;
} 


void displayStars (int rows, int cols)
{

   for (int across = 0; across < rows; across++)
     {
          if (across == 0 || across == rows)
    {
           for (int down = 0; down < cols; down++)
        cout << "*";
        cout << endl;
    }
           else
    {
            for (int down = 0; down < cols; down++)
               {
                               if (down == 0 || down == cols)
             cout << "*";
                               else
                                                  cout << "  ";
                              }
      }
       cout <<endl;
    }
}

void displayStars (int rows, int cols)
{

for (int across = 0; across < rows; across++)
{
for (int down = 0; down < cols; down++)
cout << "*";
cout <<endl;
}
}

Well... You need to check for conditions.

void drawRect(int width, int height) {
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if ((j == 0) || (j == width - 1)) {
                cout << "*";
            } else if ((i == 0) || (i == height - 1)) {
                cout << "*";
            } else {
                cout << " ";
            }
        }
        cout << endl;
    }
}

The first if statement checks if we are at the left side of the rectangle or the right. The second checks if we are at the top or the bottom. If we weren't at either the left, right, top, or bottom, then we print " ".

Okay I see! It makes sense to me! THANKS A LOT :)

One more added detail, I need to make the rectangle centered.

I need to assume the screen is 80 characters wide.


THANKS A LOT :) OH man, I'm slow at this stuff. I need to start on my history exam!

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.