Graham Wilson
Graham Wilson

Reputation: 3

What character is this?

I'm writing a simple program to generate a box with a user-defined sidelength and border/fill characters. Everything is working as I want it to, except when it prints the box to the terminal, it produces a strange character I cannot find anywhere. I feel like if I know what it is, I might be able to fix it. My header file is here:

#ifndef Box_h
#define Box_h

class Box
{
private:
    int pSidelength;
    char pBorder;
    char pFill;

public:
    Box(int pSidelength, char pBorder = '#', char pFill = '*');
    int Sidelength;
    char Border;
    char Fill;

    int Area();
    int Perimeter();
    int GetSize();
    int Grow();
    int Shrink();

    char SetBorder();
    char SetFill();

    void Draw();
    void Summary();
};
#endif Box_h

My source for the class is:

#include <iostream>
#include "box.h"
#include <iomanip>

using namespace std;

Box::Box(int pSidelength, char pBorder, char pFill)
{
    if (pSidelength < 1)
        {
            Sidelength = 1;
        }
    else if (pSidelength > 39)
        {
            Sidelength = 39;
        }
    else
    {
        Sidelength = pSidelength;
    }
    if (pBorder != '#')
    {
        SetBorder();
    }
    if (pFill != '*')
    {
        SetFill();
    }
}

int main(void)
{
    Box     MyBox1(3,'#','*');
    Box     MyBox2(7, '^', '*');
    Box     MyBox3(10, '$', '%');
    MyBox1.Grow();
    MyBox2.Shrink();

    MyBox1.Summary();
    MyBox2.Summary();
    MyBox3.Summary();

    return 0;
    }

int Box::Shrink()
{
    if (Sidelength == 1)
        {
            Sidelength = Sidelength;
        }
    else
        {
            Sidelength = Sidelength - 1;
        }
    return Sidelength;
} 

int Box::Grow()
{
     if (Sidelength == 39)
        {
            Sidelength = Sidelength;
        }
    else
        {
            Sidelength = Sidelength + 1;
        }
    return Sidelength;
}

char Box::SetFill()
{
Fill = pFill;
    return Fill;
}

char Box::SetBorder()
{
    Border = pBorder;
    return Border;
}

int Box::Area()
{
    int area = (Sidelength)*(Sidelength);
    return area;
}

int Box::Perimeter()
{
    int perimeter = 4 * (Sidelength);
    return perimeter;
}

int Box::GetSize()
{
    int size = Sidelength;
    return size;
}

void Box::Draw()
{
int j = 1;
int k = 1;

if (Sidelength == 1 || Sidelength == 2)
{
    for (int i = 1; i <= Sidelength; i++)
    {
        while (j <= Sidelength)
        {
            cout << setw(2) << Border;
            j++;
        }
        j = 1;
    }
    cout << endl;
}
else
{
    for (int i = 1; i <= Sidelength; i++)
        {
            if (i == 1 || i == Sidelength)
            {
                while (k <= Sidelength)
                {
                    cout << setw(2) << Border;
                    k++;
                }
                cout << endl;
                k = 1;
            }
            else
            {
                while (j <= Sidelength)
                {
                    if (j == 1 || j == Sidelength)
                    {
                        cout << setw(2) << Border;
                    }
                    else
                    {
                        cout << setw(2) << Fill;
                    }
                    j++;
                }
                cout << endl;
                j = 1;
            }
        }
        cout << endl;
    }
}

void Box::Summary()
    {
    cout << "The Sidelength of the box is: " << Box::GetSize() << endl;
    cout << "The Perimeter of the box is: " << Box::Perimeter() << endl;
    cout << "The Area of the box is: " << Box::Area() << endl;
    Box::Draw();
    }

The program has a default character associated with Border/Fill, as specified in the header file. When run, it produces this:

Terminal Output

What character is that, and any ideas on why it would be appearing in the first place?

Upvotes: 0

Views: 130

Answers (2)

r-sniper
r-sniper

Reputation: 1483

You got confused with same name of variables.

Box::Box(int pSidelength, char pBorder, char pFill)
{
    if (pSidelength < 1)
        {
            Sidelength = 1;
        }
    else if (pSidelength > 39)
        {
            Sidelength = 39;
        }
    else
    {
        Sidelength = pSidelength;
    }
    if (pBorder != ' ') //Here pBorder has '*' but this is local 
//                        pBorder to this Function
    {
        SetBorder();
    }
    if (pFill != ' ')
    {
        SetFill();
    }
}

And When you call SetBorder();

It makes Border as pBorder as that was declared in the class which is still unintialized.

char Box::SetBorder()
{
    Border = pBorder; //This pBorder is not initialized
    return Border;
}

Solution 1

Dont use Function

if (pBorder != ' ') 

{
    Border = pBorder;
}

Solution 2

Pass pBorder

if (pBorder != ' ') 

{
    SetBorder(pBorder);
}
char Box::SetBorder(char pBorder)
{
    Border = pBorder; //This pBorder is not initialized
    return Border;
}

Upvotes: 0

RichieHindle
RichieHindle

Reputation: 281485

The character is random, and could in theory be different every time you run the program.

It's coming from the pBorder member, which is never set to anything.

Upvotes: 1

Related Questions