1

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:

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!

Community
  • 1
  • 1
  • 2
    You shouldn't count on `for (int dots; dots <= width; dots++)` initializing `dots` to 0. Always initialize your variables. – Retired Ninja Jul 14 '15 at 03:37

5 Answers5

4

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.

Josh Vazquez
  • 300
  • 1
  • 9
1

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;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
1

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

+--------+
|        |
|        |
|        |
|        |
+--------+
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • 1
    I'd clean it up further with a lambda for 'line' that captures 'width' and takes a string of length 3 that it splits out into 3 characters. That way the box-drawing code blocks would just be: line("+-+") (etc...) – Kennet Belenky Jul 14 '15 at 16:25
  • @PamalMangat: That is called the ["goes to operator"](http://stackoverflow.com/questions/1642028/what-is-the-name-of-the-operator). It is really a just a funny rewrite of `while(w-- > 0)` (while w-Decrement is Greater than 0). – abelenky Jul 14 '15 at 16:27
  • @PamalMangat Since you're still learning to code I feel compelled to warn you that this code, while functional, is mostly a joke. It's great to learn how and why it works, but don't try to emulate it (unless your intent is to write hilariously bad code too. In that case, please emulate and build on it). – Kennet Belenky Jul 14 '15 at 16:34
  • Honestly guys I am still learning to code and am new to c++ just off of python. Any tips for a new programmer to pick up new language? @KennetBelenky –  Jul 14 '15 at 19:14
1

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

Gavinok
  • 11
  • 2
0

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<<"*";
}
Lilith Daemon
  • 1,473
  • 1
  • 19
  • 37