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<<"*";
}
Related
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;
}
I am trying to build a triangle, with a user entered base and height.
When these entered values are different (base!=height), the program goes haywire and gets stuck in the triangle draw loop.
I've tried altering the code a couple of times, but please treat me as a programming novice.
//BUILD TRIANGLE//
#include <string>
#include <iomanip>
#include <iostream>
int main()
{
std::cout << "\nEnter base and height:\n";
int height{0}; int base{0};
std::cin >> base >> height;
std::string bottom(base, '*');
std::string top = "*";
int middlerows = height - 1;
int middlespacechars;
std::cout << top << std::endl;
for (middlespacechars = 0;
middlerows != 1 || middlespacechars != base - 2;
++middlespacechars, --middlerows) {
std::string middlespace(middlespacechars, ' ');
std::cout << "*" << middlespace << "*\n";
}
std::cout << bottom << "\n" << std::endl;
std::cout << "^TRIANGLE\n";
std::cout << "BASE = " << base << std::endl;
std::cout << "HEIGHT = " << height << std::endl;
std::cout << "goodbye" << "\n" << std::endl;
}
The output is totally haywire, with asterisks across the screen in no discernible shape.
When I put in values where base=height, though, a pretty little right angle triangle pops up.
With your code, you can only draw well triangles which have base equal to height.
If you change stop condition in your for loop, you can get what you probably want to get:
for (middlespacechars = 0; middlerows != 1 || middlespacechars != base - 2; ++middlespacechars, --middlerows) {
... into ...
for (middlespacechars = 0; middlerows > 1 || middlespacechars < base - 2; ++middlespacechars, --middlerows) {
It was huge probability that if base and height are different then stop condition will not be achieved. For loop in your code will stop if middlerows will be 1 and middlespacechars will be base - 2 at the same moment.
Test it here.
//C++ program to display hollow star pyramid
#include<iostream>
using namespace std;
int main()
{
int rows, i, j, space;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
//for loop to put space in pyramid
for (space = i; space < rows; space++)
cout << " ";
//for loop to print star
for(j = 1; j <= (2 * rows - 1); j++)
{
if(i == rows || j == 1 || j == 2*i - 1)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
I have this assignment:
Write a program trapezoid.cpp that prints an upside-down trapezoid of
given width and height.
However, if the input height is impossibly large for the given width,
then the program should report, Impossible shape!
Example 1:
Input width: 12 Input height: 5
Shape:
************
**********
********
******
****
Example 2:
Input width: 12 Input height: 7
Impossible shape!
I need help trying to figure out what an impossible trapezoid would be. This is my code, so far:
#include <iostream>
using namespace std;
int main()
{
int rows, width, height, spaces, stars; // declare values
cout << "enter width" << endl;
cin >> width;
cout << "enter height" << endl;
cin >> height;
for (int row = 0; row < height; ++row) {
for (int col = height + row; col > 0; --col) {
if (height % 6 == 1) {
cout << "Impossible shape!" << endl;
return 0;
}
cout << " ";
}
for (int col = 0; col < (width - 2 * row); ++col) {
cout << "*";
spaces += 1;
stars -= 2;
}
cout << endl;
The number of asterisks in the shape starts with the width at the top level and then decreases by 2 for each subsequent level. Once the bottom level is 2 asterisks wide, you cannot have another level below it. Therefore, the maximum height for a width k, where k is an even integer, is k / 2. I will leave the case where k is an odd integer for you to determine yourself.
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.
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.