Print out a text in the middle of a rectangle - c++

I want to print out a rectangle and in the middle I want to output a preferred text.
So In the terminal it should look like:
Enter width and height: 5 6
+-----+
| |
| |
| |
| |
| |
+-----+
Then in the middle of the rectangle (height/2) it should print out "Hey". It doesn't fit in this example because the format on reddit is a bit different than my compiler.
However I wonder how I could make this happen? I can create the rectangle but I cannot seem to create a "when" statement, as in "when mid occurs, print out "Hey".
I need help finding a way to write such statement.
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
void print_row (int const width)
{
cout << '+' << '-';
for (int i = 1; i < width; ++i)
{
cout << '-';
}
cout << '+' << endl;
}
void print_rectangle (int const width, int const height)
{
int mid = height/2;
print_row(width);
for (int i {}; i < height; ++i)
{
cout << '|' << setw(width+1) << '|' << endl;
if (mid)
{
cout << "Hey" << endl;
}
}
print_row(width);
}
int main()
{
int width {};
int height {};
cout << "Enter width and height: ";
cin >> width >> height;
print_rectangle(width,height);
return 0;
}

first of all validate your width and height in your print_rectangle
String output = "hey";
if(width < output.length() || height <= 0) return;
and change the for loop like this
for (int i = 0; i < height; ++i) {
if (i == mid) {
int spacing = width - output.length();
cout <<'|'<< setw(width-spacing/2) << output << setw(spacing/2+1) << '|' << endl;
continue;
}
cout << '|' << setw(width+1) << '|' << endl;
}

Related

Print out the alphabet using for loop and setw

When I compile my program, run it and enter values I will get a rather strange and unexpected output:
So if I enter:
Enter width and height: 9 5
Enter characters: X O
1 XOXOXOXOX
2 OXOXOXOXO
3 XOXOXOXOX
4 OXOXOXOXO
5 XOXOXOXOX
A BCDEFGHI
When it's supposed to be:
Enter width and height: 9 5
Enter characters: X O
1 XOXOXOXOX
2 OXOXOXOXO
3 XOXOXOXOX
4 OXOXOXOXO
5 XOXOXOXOX
ABCDEFGHI
When do my void print_alphabet in another program it will work out just fine so I don't know the problem. I believe it has something to do with my other function but I can not seem to get it to work. Why does it act that way? Why does it print out A and then it does setw and prints out the rest?
This is my code:
#include <iostream>
#include <iomanip>
using namespace std;
void print_chess_board (int const height,
int const width,
char const char_1,
char const char_2)
{
int index {};
for (int i = 1; i <= height; ++i)
{
if (i%2)
{
index = 0;
}
else
{
index = 1;
}
cout << left << setw(3) << i;
for (int j {}; j < width; ++j)
{
if (++index%2 == 0)
{
cout << char_2;
}
else
{
cout << char_1;
}
}
cout << endl;
}
}
void print_alphabet (int const width)
{
cout << setfill(' ') << setw(4);
for (int i {}; i < width; ++i)
{
cout << char('A' + i);
}
}
int main()
{
int width {};
int height {};
char char_1 {};
char char_2 {};
cout << "Enter width and height: ";
cin >> width >> height;
cout << "Enter characters: ";
cin >> char_1 >> char_2;
print_chess_board(height,width,char_1,char_2);
print_alphabet(width);
return 0;
}
You need to change
cout << setfill(' ') << setw(4);
to
cout << right << setfill(' ') << setw(4);

Rectangular shape with characters inside it placed with a certain width

I am trying to replicate this:
Type the height and width of the rectangle: 10 13
+-------------+
| w w w |
| w w w |
| w w w |
|w w w w|
| w w w |
| w w w |
| w w w |
|w w w w|
| w w w |
| w w w |
+-------------+
Type the height and width of the rectangle: 0 0
+-+
+-+
Type the height and width of the rectangle: 4 1
+-+
| |
| |
| |
|w|
+-+
I succesfully made a subprogram that printed out the rectangle (though it is messy and there's obvious duplications of code). How can I, in the same subprogram (or maybe in a different one) print out the 'w' and the empty spaces filled with spaces? The width of each 'w' is 4 spaces. I kind of have an idea. That we do cout << setfill(' ') << setw(4) << 'w' and then an if-statement that if the setw exceeds <= width it will do a '\n'
However this is my code and I would much appreciate how my code could be improven and how to implement a character with width 4 inside the rectangle.
#include <iostream>
#include <iomanip>
using namespace std;
void print_rectangle (int const width,
int const height)
{
cout << '+' << '-';
for (int i {1}; i < width; ++i)
{
cout << '-';
}
cout << '+' << endl;
for (int j {}; j < height; ++j)
{
cout << '|' << setw(width + 1) << '|' << endl;
}
cout << '+' << '-';
for (int i {1}; i < width; ++i)
{
cout << '-';
}
cout << '+' << endl;
}
int main()
{
int width {};
int height {};
cout << "Enter the height and width of the rectangle: ";
cin >> height;
cin >> width;
print_rectangle(width,height);
return 0;
}
Well the solution for the second part which will print the 'w' and the spaces is as follows:
#include <iostream>
#include <iomanip>
using namespace std;
void print_rectangle (int const width,
int const height)
{
int counter=1;
int remainder= (width+1) % height;
cout << '+' << '-';
for (int i = 1; i < width; ++i)
{
cout << '-';
}
cout << '+' << endl;
for (int j =0 ; j < height; ++j)
{
if(j == 0 || j == height )
cout << '|' << setw(width + 1) << '|' << endl;
else
{
cout << '|';
for (int i = 0; i <width ; i++)
{
if(counter % remainder == 0)
cout<<"w";
else
cout<<" ";
counter++;
}
cout << '|'<<endl;
}
}
cout << '+' << '-';
for (int i =1 ; i < width; ++i)
{
cout << '-';
}
cout << '+' << endl;
}
int main()
{
int width =0;
int height =0;
cout << "Enter the height and width of the rectangle: ";
cin >> height;
cin >> width;
print_rectangle(width,height);
return 0;
}
I have tested the given input cases on it as well and it gives the right output. What's happening here is that the remainder of height and width+1 gives us a remainder and with the help of that remainder we are going to print w's. A counter is incremented in the loop and whenever that counter is a multiple of that remainder w will be printed otherwise space.
Here is the output of the test case input 4 and 1 output
And here is the output of test case input 10 and 13
Please learn split problem to smaller pieces which are a small functions.
Take a look on this:
using std::setw;
void print_ending(std::ostream& out, int const width) {
out << '+' << std::string(width, '-') << '+' << '\n';
}
void print_row(std::ostream& out,
int const width,
int const period,
int offset) {
out << '|' << std::string(std::min(width, offset), ' ');
if (offset < width) {
out << 'w';
auto remainin_width = width - offset - 1;
for (int i = remainin_width / period; i > 0; --i) {
out << setw(period) << 'w';
}
out << std::string(remainin_width % period, ' ');
}
out << "|\n";
}
void print_rectangle(std::ostream& out,
int const width,
int const height,
int period = 4) {
print_ending(out, width);
for (int j{}; j < height; ++j) {
print_row(out, width, period, (height - j) % period);
}
print_ending(out, width);
}
Live demo

C++ Print A Box Application

I'm currently working on a c++ project that prints a box to the screen based on the user's entered width and height input. I can so far print out the top, bottom, and left side dots on the screen. The only thing i need help with is printing out the far right dots. I've provided a screen shot of the output below the code.
CODE:
#include <iostream>
using namespace std;
int main()
{
int width;
int height;
//Introduction:
cout<<"Welcome to the [Draw A Rectangle] program!\n";
cout<<"This program will draw a rectangle in the application\n";
cout<<"You will have to enter the width and the height and it will draw it\n";
//User enters box width and height:
cout<<"Please enter a width: ";
cin>>width;
cout<<"Please enter a height: ";
cin>>height;
//Prints the top dots (horizontal):
for (int dots; dots <= width; dots++)
{
cout<<"*";
}
//Prints the left dots (vertical):
for (int dots; dots < height; dots++)
{
cout<<"*\n";
}
//Prints the bottom dots (horizontal):
for (int dots; dots <= width + 1; dots++)
{
cout<<"*";
}
//Keeps program running:
cin.get();
}
Screenshot:
As you can see the dots are not printing on the far right side and the box is incomplete, and I need this fixed, anything helps, Please!
Based on the width entered, when you insert the left dot, insert an appropriate number of spaces and then the right dot. Put the newline after the right dot.
I had some fun with it:
See IDEOne Link
#include <stdio.h>
int main(void) {
int height = 10;
int width = 10;
int it, in;
it=width;
while (it --> 0) putchar('*'); putchar('\n');
it = height-2;
while(it --> 0)
{
in = width-2;
putchar('*'); while(in --> 0) putchar(' '); putchar('*'); putchar('\n');
}
it=width;
while (it --> 0) putchar('*'); putchar('\n');
return 0;
}
Perhaps a simpler version:
(but likely a bit obfuscated to most C programmers)
#include <stdio.h>
void line(char s, int w, char b, char e)
{
{ putchar(s); }
while(w --> 0) { putchar(b); }
{ putchar(e); }
{ putchar('\n'); }
}
int main(void)
{
int width = 10;
int height = 6;
width -= 2;
height-= 2;
{ line('+', width, '-', '+'); }
while(height --> 0) { line('|', width, ' ', '|'); }
{ line('+', width, '-', '+'); }
return 0;
}
Output
+--------+
| |
| |
| |
| |
+--------+
This solution may be 5 years late, but does correct the problems with the original program.
It checks that the user input is within acceptable values for the 80x24 output of a standard terminal and that the dimensions are positive. Big values are cropped to the max 80x24 chars and the absolute value of negative numbers entered are used.
NOTE: this program does NOT check if non integer values are entered.
Other than the user input verification, this solution rectifies the errors in the original code and uses c++'s std::endl instead of c's \n for a new line.
If you are only interested in the code to print the box look for the code after the comment:
//3. Print the box
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
int width = 0;
int height = 0;
// 1. Get User Input
//Introduction:
cout << "Welcome to the [Draw A Rectangle] program!" << endl;
cout << "This program will draw a rectangle in the application" << endl;
cout << "You will have to enter the width and the height"
<< " and it will draw it" << endl;
//User enters box width and height:
cout << "Please enter the width: ";
cin >> width;
cout << "Please enter the height: ";
cin >> height;
// 2. Verify Validity of User Input
// Height Max 24 width max 80 not zero or negative values
// 2a. check that width is not zero or negative.
if (width < 1) // zero or negative
{
if (width < 0)
{
cout << "A rectange must have a positive width [" << width
<< "] set to " << abs(width) << "." << endl;
width = abs(width);
}
else // width == zero
{
cout << "A rectangle must had a width of 1 or more."
<< " Width [" << width << "] set to 1." << endl;
width = 1;
}
}
// 2b. check that height is not zero or negative.
if (height < 1)
{
if (height < 0)
{
cout << "A rectange must have a positive height [" << height
<< "] set to " << abs(height) << "." <<endl;
height = abs(height);
}
else // height == zero
{
cout << "A rectangle must had a height of 1 or more."
<< " Height [" << height << "] set to 1." << endl;
height = 1;
}
}
// 2c. Limit to 80x24 chars.
// The standard vt100 terminal was only 80x24 chars
// 2c i) check width 80 or less
if (width > 80)
{
cout << "Width must be 80 or less. Width [" << width
<< "] limited to 80." << endl;
width = 80;
}
// 2c ii) check height 24 or less
if (height > 24 )
{
cout << "Height must be 24 or less. Height [" << height
<< "] limited to 24." << endl;
height = 24;
}
// 3. Print the box
//Prints the top dots (horizontal):
for (int dots = 0; dots < width; dots++)
{
cout << "*";
}
cout << endl;
//Prints the left dots (vertical):
// note first and last row are rows of dots
if (height > 1 )
{
for (int dots = 0; dots < height-2; dots++) // first row already printed
{
cout << setw(1) <<"*";
if (width > 1 )
{
cout << setw(width-1) << right << "*" << endl;
}
else
cout << endl;
}
//Prints the bottom dots (horizontal):
for (int dots = 0; dots < width; dots++)
{
cout<<"*";
}
cout << endl;
}
//Keeps program running:
cin.get();
}
Sceenshot
You can output spaces after the vertical line. Here is a modified version of the code to accomplish this task:
//Prints the top dots (horizontal):
for (int dots = 0; dots < width; dots++)
{
cout<<"*";
}
cout<<endl;
//Prints the left dots (vertical):
for (int dots = 0; dots < height-2; dots++)
{
cout<<"*";
for(int i = 0; i < width-2; ++i) cout<<" ";
cout<<"*"<<endl;
}
//Prints the bottom dots (horizontal):
for (int dots = 0; dots < width; dots++)
{
cout<<"*";
}

Draw A Rectangle With Asterisks

I am trying to write a C++ Program to display a rectangle drawn in asterisks. I have the program running properly except for the fact that only one side of the heights of my rectangles print. Here is the code I have currently written for the display rectangle method.
void Rectangle::displayRectangle()
{
int i=0, j=0;
for (int i = 0; i < width; i++)
{
cout << "*";
}
cout << endl;
for (int i = 0; i < height - 2; i++)
{
cout << "*";
for (int j = 0; j < width; j++)
{
cout << " ";
}
cout << endl;
}
for (int i = 0; i < width; i++)
{
cout << "*";
}
cout << endl;
}
Specify a width and height at the start then you only need 3 loops. The first will print the top line of the rectangle. The second will print both sides of the rectangle (minus the very top and very bottom of the sides). The third will print the bottom line of the rectangle.
Like so
// Width and height must both be at least 2
unsigned int width = 7; // Example value
unsigned int height = 5; // Example value
// Print top row
for(unsigned int i = 0; i < width; i++);
{
std::cout << "*";
}
std::cout << std::endl;
// Print sides
for(unsigned int i = 0; i < height - 2; i++)
{
std::cout << std::setw(width - 1) << std::left << "*";
std::cout << "*" << std::endl;
}
// Print bottom row
for(unsigned int i = 0; i < width; i++)
{
std::cout << "*";
}
std::endl;
You will need to include both iostream and iomanip for this to work (setw is part of iomanip).
The top and bottom rows could also be done using the method to fill spaces with a given character, but I cannot recall that method right now.
This can be done much easier and clearer.
The logic here is to draw from line to line, so you only need one loop
(I chose to use the auto specifier in this example because I think it looks neater and used often in modern c++, if your compiler doesn't support c++11, use char, int etc.):
int main()
{
using namespace std;
auto star = '*';
auto space = ' ';
auto width = 20;
auto height = 5;
auto space_cnt = width-2;
for (int i{0}; i != height+1; ++i) {
// if 'i' is the first line or the last line, print stars all the way.
if (i == 0 || i == height)
cout << string(width, star) << endl;
else // print [star, space, star]
cout << star << string(space_cnt, space) << star << endl;
}
}
Well, you don't see the second vertical line, because you don't draw it in your line loop.
void DrawRect(int w, int h, char c)
{
cout << string(w, c) << '\n';
for (int y = 1; y < h - 1; ++y)
cout << c << string(w - 2, ' ') << c << '\n';
cout << string(w, c) << '\n';
}
Try to prompt the user for the number of rows and columns. Then, using nested loops, display a rectangle of stars based on the user input.

How to make exception in for loop?

The code below prints a box with the intergers the user inputs. I need to make it hollow to only display the full length of the first and last line of the box. like width = 5 height = 4
Example Output:
00000
0 0
0 0
00000
Source:
int main ()
{
int height;
int width;
int count;
int hcount;
string character;
cout << "input width" << endl;
cin >> width;
cout << "input height" << endl;
cin >> height;
cout << "input character" << endl;
cin >> character;
for (hcount = 0; hcount < height; hcount++)
{
for (count = 0 ; count < width; count++)
cout << character;
cout << endl;
}
}
I do not know how to change the loop condition for the width to make it work.
I think you can test whether you are in the first or last row, and first or last column.
Example:
#include <string>
#include <iostream>
int main ()
{
using namespace std; // not recommended
int height;
int width;
string character;
cout << "input width" << endl;
cin >> width;
cout << "input height" << endl;
cin >> height;
cout << "input character" << endl;
cin >> character;
for (int i = 0; i < height; i++)
{
// Test whether we are in first or last row
std::string interior_filler = " ";
if (i == 0 || i == height - 1)
{
interior_filler = character;
}
for (int j = 0; j < width; j++)
{
// Test whether are in first or last column
if (j == 0 || j == width -1)
{
cout << character;
} else {
cout << interior_filler;
}
}
// Row is complete.
cout << std::endl;
}
}
Here is the output:
$ ./a.out
input width
10
input height
7
input character
*
OUTPUT
**********
* *
* *
* *
* *
* *
**********
Add an if to the cout << character line. If we're not in the first row or column, output a space instead of the character.