I'm trying to write a method that draws a rectangle. If the user enters height = 4, and width = 8, and the print of character " * ", then the left rectangle is drawn on the left if the filled flag is true. If the flag is false, then version on the right is drawn which would be an empty rectangle.

* * * * * * * * ||||| * * * * * * * *
* * * * * * * * ||||| *e m p t y *
* * * * * * * * ||||| *e m p t y *
* * * * * * * * ||||| * * * * * * * *


I've written the below so far, where am I going wrong?:

rec()
{
if (filled == true)
{
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
cout << "*";
}
cout << endl;

}
}
else // this is the empty rectangle
{
for (int row = 0 ; row < height; row++)
{
for (int col = 0; col < width; col++)
{
cout << "*" << setw(width-1) << "*";
}
cout << endl;
}
}

Recommended Answers

All 12 Replies

Welcome to DaniWeb! To allow us to help most effectively, please use code tags when you post code. Also, you should always comment code as much as you can. When you post code that isn't working, you should also show the incorrect output (or any errors that occur). I also suggest that you make a fully compilable example that demonstrates the problem (i.e. include main(), and hardcode values rather than taking user input).

Good luck,

David

I would put an if statement in your second for loop for the empty rectangle and check to see if you are not on the top or the bottom of the rectangle. If you are not on the top or bottom then only place an Astrix on the ends otherwise place all Astrix.

try this

void rec(int &right,int&left){
	for(int x =0;x<right;x++){
		for(int i=0;i<left;i++){
			cout <<"*";
		}
		cout <<"*\n";
	}

}
int main(){
int l=10,r=20;
rec(l,r);
return 0;
}

@ richieking He already has the algorithm to make a solid rectangle he is trying to make an empty one. Also what you have puts an extra Astrix on each line so your rectangle would be 10x21.

i might as well write up all the code, so that you get the bigger picture:

class Rec
{
      pri:
              int height;
              int width;
              char c;
              bool filled;
              
      pub:
             Rect ()
             {
                       height = 0;
                       width = 0;
                       filled = true;
             }
                       
             
             Rec (int h, int w, char c, bool f)
             {
                       height = h;
                       width = w;
                       ch = c;
                       filled = f;
             }
            
                       
             void set (int h, int w, char c)
             {
                       height = h;
                       width = w;
                       ch = c;
                       
             }
             
             void flip () // swapping height with width and viceversa
             { 
                  int height, h , width, w;
                  
                  height = h;
                  width = w;
                  width = height;
                  width = h;
                  height = w;
                       
             }
                            
             
             void toggle () //????????????????????????????????
             {
                       if (filled)
                       {
                           filled = false;
                       }
                       else
                       {
                           filled = true;
                       }
             }
                       
             int getHeight()
             {
                       return height;
             }
             int getWidth()
             {
                       return width;
             }
             int getArea ()
             {
                       return (height * width);
             }
              
              void drawrec();       
};

void Rec::drawrec() //
             {
                        if (filled == true) 
                       { 
                        for (int row = 0; row < height; row++)
                        { 
                            for (int col = 0; col < width; col++)
                            {
                                cout << "*";
                            }
                            cout << "*";
                            cout << endl;
                            
                         }
                         }
                         else //this is where i'm finding it confusing
                        {
                         for (int row = 0 ; row < height; row++)
                         {
                            for (int col = 0; col < width; col++)
                            {
                                
                               cout << "*" << setw(width-1) << "*";
                            }
                               cout << endl;  
                         }
                         }
}
int main ()
{
    char tempCh;
    int tempHeight, tempWidth;
    
    Rec rec1;
    
    cout << " Enter Height: " << endl;
    cin >> tempHeight;
    
    cout << " Enter Width: " << endl;
    cin >> tempWidth;
    
    cout << " Enter Character: " << endl;
    cin >> tempCh;
    
    rec1.set (tempHeight, tempWidth, tempCh);
    
    cout << " Display Height: " << rec1.getHeight() << endl;
    cout << " Display Width: " << rec1.getWidth() << endl;
    cout << " Display Area: " << rec1.getArea() << endl;
    
    rec1.flip (); // ????
             
                       
    rec1.drawrec();
    

    Rec rec2 (10 , 20, '*' , true); // ??????
    
    rec2.drawrec();    //i'm not sure i'm doing this correctly
    
    cout << " Enter Height: " << endl;
    cin >> tempHeight;
    
    cout << " Enter Width: " << endl;
    cin >> tempWidth;
    
    cout << " Enter Character: " << endl;
    cin >> tempCh;
    
    rec2.set (tempHeight, tempWidth, tempCh);
    
    cout << " Display Height: " << rec2.getHeight() << endl;
    cout << " Display Width: " << rec2.getWidth() << endl;
    cout << " Display Area: " << rec2.getArea() << endl;
    
    rec2.flip (); // ????
             
                       
    rec2.drawrec();
    
    system ("PAUSE");
    
    return 0;
}

@ richieking He already has the algorithm to make a solid rectangle he is trying to make an empty one. Also what you have puts an extra Astrix on each line so your rectangle would be 10x21.

try and run it and see it first ok?

i have...runs successfully, but the unfilled square does not appear

stan2000 plz put your code in the code tags

brute force here....

#include <iostream>



using namespace std;



void rec(int &right,int&left){
	if(right != left){
		cout <<" left and right must be the same int value";
		return;
	}
	for(int a=0;a<right+1;a++)
	{
		cout <<"* ";
	}
	cout<<endl;
	for(int x =0;x<right;x++){
		cout <<"*";
		for(int i=0;i<left;i++){
			cout <<"  ";

		}

		cout <<"*\n";
	}
	for(int a=0;a<right+1;a++)
		{
			cout <<"* ";
		}
}

int main(){
	int left,right;
	cout <<"Your width and height int values must be the same. \n";
	cout <<"Example width=30, height=30\n";
	cout <<"Enter the width\n";
	cin>> left;
	cout <<"Enter the height\n";
	cin>>right;
	rec(left,right);



	return 0;

}

######## output   ####
Your width and height int values must be the same. 
Example width=30, height=30
Enter the width
10
Enter the height
10
* * * * * * * * * * * 
*                    *
*                    *
*                    *
*                    *
*                    *
*                    *
*                    *
*                    *
*                    *
*                    *
* * * * * * * * * * *

updated.
on lines 16 and 30... do this

cout<<"**";

to both numbers.
It will produe a better draw.

==out put==

10
Enter the height
10
**********************
*                    *
*                    *
*                    *
*                    *
*                    *
*                    *
*                    *
*                    *
*                    *
*                    *
**********************
That still didn't answer my question. I was hoping that an if statement would be inserted in the second else which would create an unfilled rectangle.


void Rectangle::drawrec() 
{
if (filled = true) 
{ 
for (int row = 0; row < height; row++)
{ 
for (int col = 0; col < width; col++)
{
cout << "*";
}

cout << endl;

}
}
else

{
for (int row = 0 ; row < height; row++)
{
for (int col = 0 ; col < width ; col++)

{
if (height != width)
cout << " * ";

}

}

}
commented: CODE Tags are only for code, not your information. And FORMATting your code makes it possible to follow it. -3

Well i just gave you an idea. carry on from there.
:)

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.