这个是什么角色?

时间:2017-03-19 15:22:55

标签: c++

我正在编写一个简单的程序来生成一个带有用户定义的边长和边框/填充字符的框。一切都按照我想要的方式工作,除非它将盒子打印到终端时,它会产生一个我在任何地方都找不到的奇怪角色。我觉得如果我知道它是什么,我可以解决它。我的头文件在这里:

#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

我的课程来源是:

#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();
    }

程序具有与边框/填充相关联的默认字符,如头文件中所指定。运行时,它产生:

Terminal Output

这是什么性格,以及为什么它首先出现的任何想法?

2 个答案:

答案 0 :(得分:1)

角色是随机的,理论上每次运行程序时都可能不同。

它来自from mysql.connector import (connection) from datetime import * from RPI1_ui import Ui_Form '''Handling of database related stuff''' class DatabaseUtility(Ui_Form): def __init__(self): #DB Connection self.cnx = None self.cursor = None def mysql_connect(self): # Database connection try: self.cnx = connection.MySQLConnection(user='root', password='', host='127.0.0.1', database='spicadb') self.cursor = self.cnx.cursor() except connection.errors.InterfaceError as e: self.lblShowInfo.setText(str(e)) # -> Try to change label def get_table(self): return self.run_command("SELECT * FROM tblseal") def get_columns(self): return self.run_command("SHOW COLUMNS FROM tblseal") 成员,该成员从未设置为任何内容。

答案 1 :(得分:0)

你对变量的相同名称感到困惑。

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();
    }
}

当您致电SetBorder();

它使BorderpBorder,因为在类中声明了它仍然是未初始化的。

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

解决方案1 ​​

不要使用功能

if (pBorder != ' ') 

{
    Border = pBorder;
}
  

解决方案2

传递pBorder

if (pBorder != ' ') 

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