Hi, I'd like to know how to draw a rectangle using the following:

Prompt the user to enter the character to use for drawing the rectangle and promt the user to enter the number of columns and the number of rows for the rectangle.

Furthermore, the max for the rectangle has to be up to 22 rows by up to 79 columns. Ifthe user enters a bigger value cout << "Invalid input" << endl; I already have a little something

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

int main(void){

int col, rows; char char1;


cout << "Enter the  character to be used for drawing the rectangle: " << endl;
cin >> char1;

cout << "Enter the number of columns (up to 79): " << endl;
cin >> col;

cout << "Enter the number of rows  (up to 22): " << endl;
cin >> rows;

//Here is when you help me drawing the rectangle :)

return 0;
}
//Here is when you help me drawing the rectangle :)

Funny. :)

Assuming a solid rectangle, you have nested loops: outer loop counts rows, inner loop counts columns; inner loop contains output of char1, at the end of the inner loop you output a newline.

A typical loop would be such like this.

for ( int r = 0; r < rows; ++r ) { /* loop body */ }

Thanks for a quick reply!, however I'm VERY new to C++. So, I will appreciate whoever is able to elaborate a little bit more.

Orium.

Assuming a solid rectangle, you have nested loops: outer loop counts rows, inner loop counts columns; inner loop contains output of char1, at the end of the inner loop you output a newline.

A typical loop would be such like this.

for ( int r = 0; r < rows; ++r ) { /* loop body */ }

Thanks for a quick reply!, however I'm VERY new to C++. So, I will appreciate whoever is able to elaborate a little bit more.

Orium.

A nested loop means a loop within a loop:

for (row = 0; row < 3; row++)
{
    for (col = 0; col < 5; col++)
    {
        // here you output your character
    }
    // here you output a newline to finish off the row
}

The inner loop would display an entire row of characters, and the outer loop controls the number of rows you want to print. The above might print something like:
#####
#####
#####

Ok, I got the nested part.

Now, the code has to draw a rectangle outline only.
So, if user responds to the prompts :
character: #
columns: 10
rows: 5
The output should be:

##########
##########
##########
##########
##########

where the red characters are not supposed to be there.

simple -- print spaces instead of '#' characers. We aren't going to actually give you the code, so try it and post your code if you have more problems/questions.

About the spaces it might be worth making a string to do them rather than looping:

#include <iostream>
#include <string>

int main( void ) {
  std::string spaces( 10, ' ' );
  std::cout<< '#' << spaces << "#\n";

  return 0;
}

*checks OP's restrictions*

commented: good idea +19

Thanks Ancient Dragon, but I dont expect someone to just give me the code. I got that message before I subscribed.

I just want someone who can tell me how to approach, since my book is of no help.

I think to draw a rectangle we have to have a minimum of 3 columns and 3 rows
###
###
###

Lets pretend the red is invisible.

I imagine I can separate the code among the top the middle and the bottom lines.

Now with the char1 being # and col being 5, how do I print

#####

in the output?

Orium.

Reading this post reminds me of the old IBM motto:
*THINK*
Also, the analogy of a man standing amidst lumber, nails, and carpentry tools asking: " What now?"

;-)

You really have all the info you need. I don't think we should help you till you show us at least a new attempt.

Here's where I am so far

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

int main(void){ 

	int xR,yC; char char1;

	cout << "Character" << endl;
cin >> char1;

cout << "Number of columns: ";
cin >> yC;

cout << "Number of rows: ";
cin >> xR;


for ( int y = 0; y < yC; y++ )
	{
		cout << char1;
	}
for ( int x = 0; x < xR; x++ )
	{
		cout << char1 << endl;
	}


}

Orium

Nested means one loop inside the other, not one loop after the other.

I left some comments:

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

int main(void){ 

  char char1;
  int xR,yC; 

  cout << "Character" << endl;
  cin >> char1;

  cout << "Number of columns: ";
  cin >> yC;

  cout << "Number of rows: ";
  cin >> xR;


  for ( int y = 0; y < yC; y++ )
  {
      cout << char1; 
  }
  for ( int x = 0; x < xR; x++ )
  {
      cout << char1 /* BUFFER WITH SPACES AND PRINT char1 AGAIN */<< endl;
  }
  // PRINT THE FIRST ONE AGAIN


}

Also look at this:

#include <iostream>
#include <string>

int main(void){ 
  char bc = '#';
  
  std::string bt( 5, bc ), buf( 3, ' ' );

  std::cout<< bt << "\n";
  std::cout<< bc << buf << bc << "\n";
  std::cout<< bt << "\n";

  return 0;
}

I got it!

All I need is to put limits of 79 for columns and 22 for rows

Yes. FOr the third part don't endl every iteration. Just print a new line before the for loop.

Thanks guys. I got my program working just as I wanted.

Orium.

Just in time then. Loops only nest from March to September, then they begin their migration south for the winter :icon_biggrin:

OK, below is the code I created for the rectangle.
Now I need a hint on how to do it using a class file.

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::cin;
using std::endl;

int main(void){ 

int MyRow, MyCol; char MyChar; //this are my variables

cout << "WELCOME TO THE RECTANGLE PROGRAM" << endl << endl;
	

cout << "Enter any character to be the outline of the rectangle: "; //this will prompt for a character to be used for drawing the rectangle
cin >> MyChar;

cout << "Enter the number of columns (up to 79): "; //this will prompt for the number of columns the rectangle will have
cin >> MyCol;

cout << "Enter the number of rows (up to 22): "; //this will prompt for the number of rows the rectangle will have
cin >> MyRow;

string spaces( MyCol-2, ' ' ); // this string "spaces" will draw as many spaces as the user input for columns minus 2

if ((MyCol <= 79) && (MyRow <= 22)){ //this "if" is to limit the number of columns and rows to 79 and 22 respectively
	
	for ( int y = 0; y < MyCol; y++) //this "for" loop will draw the top row
		{
			cout << MyChar;
		}

	for ( int y = 2; y < MyRow; y++) //this "for" loop will draw the rows in the middle
		{
			cout <<"\n"<< MyChar << spaces << MyChar; //it consist of a starting character, empty spaces, and an ending character
		}
	cout<< endl; //this creates a new line for the bottom row

	for ( int y = 0; y < MyCol; y++) //this is the bottom row. Just like the top row
		{
			cout  << MyChar;
		}

	cout<< endl; //this creates a new line for any message to appear below the rectangle

	}
else //if the user inputs any value greater than the maximum specified, the program will show an "Invalid input" message and wont execute
cout << "Invalid Input!  " << "Check the maximum value for columns and rows" <<endl << endl;

return 0;// I didn't specified any minimum value for the number of columns and rows not because I didn't think aboutit, but becaus eit was not part of the assignment
}
/*
OUTPUT

WELCOME TO THE RECTANGLE PROGRAM

Enter any character to be the outline of the rectangle: #
Enter the number of columns (up to 79): 79
Enter the number of rows (up to 22): 22
###############################################################################
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
#                                                                             #
###############################################################################
Press any key to continue . . .
------------------------------------------------------------------------------
WELCOME TO THE RECTANGLE PROGRAM

Enter any character to be the outline of the rectangle: #
Enter the number of columns (up to 79): 80
Enter the number of rows (up to 22): 22
Invalid Input!  Check the maximum value for columns and rows

Press any key to continue . . .
------------------------------------------------------------------------------
WELCOME TO THE RECTANGLE PROGRAM

Enter any character to be the outline of the rectangle: #
Enter the number of columns (up to 79): 79
Enter the number of rows (up to 22): 23
Invalid Input!  Check the maximum value for columns and rows

Press any key to continue . . .
*/

Thanks,
Orium.

Well what do you need in the class? You need a variable to store the length, the width and the character to define the border. You'll want to be able to set all those parameters, so you can introduce some 'setters', you could maybe overload the ostream >> operator. You might want to see what values you're calling. You'll want a printing method and maybe an ostream << operator too.

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

int main(int argc, char *argv[])
{                               
   const int NUM_ACROSS = 6;    // Number of asterisks to print across
   const int NUM_DOWN = 8;    // Number of asterisks to print down
   int row; // Loop control for row number
   int column;  // Loop control for column number

for (column = 0; column <= NUM_ACROSS - 1; column++) //top line
        {
          cout << "* ";
          }

        for (row = 1; row <= NUM_DOWN; row++) // left line
        {
         cout << "\n*";
         }

        for (column = 0; column != NUM_ACROSS - 1; column++) 
         {
        cout << " *"; // Bottom Line
        }           


 cout << endl;


   system("PAUSE");
   return EXIT_SUCCESS;
} // End of main()
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.