C++ 需要帮助解决我程序中的小问题吗

C++ 需要帮助解决我程序中的小问题吗,c++,rectangles,C++,Rectangles,这是我的代码: #include <iostream> #include <string> #include <vector> using namespace std; class Point { private: double px; double py; public: void setX(const double x); void setY(const double y); double getX() con

这是我的代码:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Point
{
private:
    double px;
    double py;

public:
    void setX(const double x);
    void setY(const double y);
    double getX() const;
    double getY() const;
};

class Rectangle
{
private:
    string name;
    Point blPoint;
    double length, height;

public:
    // member functions
    void setName(const string & inName);
    void setBottomLeft(const double x, const double y);
    void setDimensions(const double inLength, const double inHeight);

    string getName() const;
    Point getBottomLeft() const;
    double getLength() const;
    double getHeight() const;

    double area() const;
    double perimeter() const;
    Point midPoint() const;
    void scaleBy2();
    void display() const;
};

// FUNCTION PROTOTYPES GO HERE:
void welcome();
bool read_rec(const string promptName, const string errorInvalid, const string errorUsed, string & inName, vector<Rectangle> & list);
void read_coord(const string promptPoint, double & x, double & y);
void read_length(const string promptLength, double & inLength, double & inHeight);
void add_rec(const string Name, double x, double y, double inLength, double inHeight, vector<Rectangle> & list);

int main()
{
    // Define your local variables, e.g. a vector of class Rectangle
    Rectangle rec;
    vector<Rectangle> list;
    string prompt1stName = "Enter the name of the first rectangle: ";
    string promptName = "Enter the name of the next rectangle: ";
    string errorInvalid = "Invalid input. Type 'rec' following by the name or 'stop' if done.";
    string errorUsed = "This name is already being used!";
    string inName;
    string Name;

    // Display welcome banner
welcome();

    /* Prompt user for first rectangle or 'stop' */
    bool read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list);

    // WHILE user input is invalid
    while (read == false)
    {

        // Display "Try again! "
 cout << "Try again! " << endl;
         read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list);
    }

    // IF user input is not 'stop'
        if (inName != "stop")
    {
        // Extract rectangle name from user input
                int a = inName.length() - 4;
        Name = inName.substr(4, a);

        // Prompt for bottom left point
                double x, y;
        string promptPoint = "Enter " + Name + "'s bottom left x and y coords: ";
        read_coord(promptPoint, x, y);

        // Prompt for length and height
                double inLength, inHeight;
        string promptLength = "Enter " + Name + "'s length and height: ";
        read_length(promptLength, inLength, inHeight);

        // Add rectangle to the rectangle list
        add_rec(Name, x, y, inLength, inHeight, list);
    }
    /* Prompt user for next rectangle or 'stop' */
    // WHILE user input not 'stop'
        while (inName != "stop")
    {
        // Display "Thank you! "
cout << "Thank you! ";
bool read = read_rec(promptName, errorInvalid, errorUsed, inName, list);

        // WHILE user input is invalid while (read == false)
        {

            // Display "Try again! "
                        cout << "Try again! " << endl;
            read = read_rec(promptName, errorInvalid, errorUsed, inName, list);
        }

            // IF user input is not 'stop'
                    if (inName != "stop")
        {

                // Extract rectangle name from user input
                            int a = inName.length() - 4;
            Name = inName.substr(4, a);

                // Prompt for bottom left point
                            double x, y;
            string promptPoint = "Enter " + Name + "'s bottom left x and y coords: ";
            read_coord(promptPoint, x, y);

                // Prompt for length and height
                            double inLength, inHeight;
            string promptLength = "Enter " + Name + "'s length and height: ";
            read_length(promptLength, inLength, inHeight);


                // Add rectangle to the rectangle list
            add_rec(Name, x, y, inLength, inHeight, list);
        }
    }

    // IF the rectangle list is not empty
        if (list.size() != 0)
    {
        // Display all rectangles in the rectangle list
int rec_num = 0;
int i = 1;
while (i< list.size())
{
    rec_num++;
    i++;

}
            cout << "You have " << rec_num+1 << " rectangle(s) in your list: ";
            cout << endl;

                for (int i = 0; i < list.size(); i++)
        {
            cout << "Rectangle '" << list[i].getName() << "' : ";
            list[i].display();
            list[i].scaleBy2();
            cout << "     After scale by 2: ";
            list[i].display();
            cout << endl;
        }
    }

    // ELSE
    else
        {
        // Display that no rectangles are in the list
                cout << "You have no rectangles in your list." << endl;
    }

  return 0;
}

// FUNCTION DEFINITIONS GO HERE:
void welcome()
{
    cout << "Welcome! Create your own list of rectangles." << endl;
    cout << "You will be asked to provide information about each rectangle in your list by name." << endl;
    cout << "Type the word 'stop' for the rectangle name when you are done." << endl;
    cout << endl;
}

bool read_rec(const string promptName, const string errorInvalid, const string errorUsed, string & inName, vector<Rectangle> & list)
{
    cout << promptName;
    getline(cin, inName);

    if (inName == "stop")
    {
        return(true);
    }
    else if (inName.substr(0,4) != "rec ")
    {
        cout << errorInvalid;
        return(false);
    }
    else
    {
        int j = 0;
        for (int i = 0; i < list.size(); i++)
        {
            if (inName == "rec " + list[i].getName())
            {
                j = j+1;
            }
        }
        if (j == 0)
        {
            return(true);
        }
        if (j != 0)
        {
            cout << errorUsed;
            return(false);
        }
    }
}

void read_coord(const string promptPoint, double & x, double & y)
{
    cout << promptPoint;
    cin >> x;
    cin >> y;
    }

void read_length(const string promptLength, double & inLength, double & inHeight)
{
    cout << promptLength;
    cin >> inLength;
    cin >> inHeight;
    cout << endl;

    while (inLength <= 0 || inHeight <= 0)
    {
        cout << "Make length and height positive values. Try again.";
        cout << promptLength;
        cin >> inLength;
        cin >> inHeight;
        cout << endl;
    }
}

void add_rec(const string Name, double x, double y, double inLength, double inHeight, vector<Rectangle> & list)
{
    Rectangle rec;
    rec.setName(Name);
    rec.setBottomLeft(x, y);
    rec.setDimensions(inLength, inHeight);
    list.push_back(rec);
}

// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:

void Point::setX(const double x)
{
    px = x;
}

void Point::setY(const double y)
{
    py = y;
}

double Point::getX() const
{
    return (px);
}

double Point::getY() const
{
    return (py);
}

void Rectangle::setName(const string & inName)
{
    name = inName;
}

void Rectangle::setBottomLeft(const double x, const double y)
{
    blPoint.setX(x);
    blPoint.setY(y);
}

void Rectangle::setDimensions(const double inLength, const double inHeight)
{
    length = inLength;
    height = inHeight;
}

string Rectangle::getName() const
{
    return (name);
}

Point Rectangle::getBottomLeft() const
{
    return (blPoint);
}

double Rectangle::getLength() const
{
    return (length);
}

double Rectangle::getHeight() const
{
    return (height);
}

double Rectangle::area() const
{
    // area = length * height
    return(length * height);
}

double Rectangle::perimeter() const
{
    // perimeter = 2 * (length + height);
    return(2 * (length + height));
}

Point Rectangle::midPoint() const
{
    Point midPoint;
    double mx = blPoint.getX() + 0.5 * length;
    double my = blPoint.getY() + 0.5 * height;
    midPoint.setX(mx);
    midPoint.setY(my);
    return(midPoint);
}

void Rectangle::scaleBy2()
{
    double midx = blPoint.getX() + 0.5 * length;
    double midy = blPoint.getY() + 0.5 * height;
    double newblPx = midx - length;
    double newblPy = midy - height;
    length = 2*length;
    height = 2*height;
    blPoint.setX(newblPx);
    blPoint.setY(newblPy);
}

void Rectangle::display() const
{
    cout << " Location is (" << blPoint.getX() << ", " << blPoint.getY() << "), length is " << length << ", height is " << height << "; Area is " << area() << "; perimeter is " << perimeter() << ", midpoint is located at (" << midPoint().getX() << ", " << midPoint().getY() << ")" << endl;
}
#包括
#包括
#包括
使用名称空间std;
类点
{
私人:
双px;
双py;
公众:
无效集x(常数双x);
void setY(const double y);
双getX()常量;
双getY()常数;
};
类矩形
{
私人:
字符串名;
点BL点;
双倍长度、高度;
公众:
//成员函数
void setName(常量字符串和inName);
void setBottomLeft(常数倍x,常数倍y);
空隙设置尺寸(长度为常数倍,高度为常数倍);
字符串getName()常量;
点getBottomLeft()常量;
双getLength()常量;
双getHeight()常量;
双面积常数;
双周长常数;
点中点()常数;
空标度b2();
void display()常量;
};
//功能原型如下:
无效欢迎();
bool read_rec(常量字符串promptName、常量字符串errorInvalid、常量字符串errorUsed、字符串和inName、向量和列表);
无效读取坐标(常量字符串提示点、双精度x、双精度y);
无效读取长度(常量字符串长度、双倍和内长、双倍和内高);
void add_rec(常量字符串名称、双x、双y、双长度、双高度、向量和列表);
int main()
{
//定义局部变量,例如类矩形的向量
矩形rec;
向量表;
string prompt1stName=“输入第一个矩形的名称:”;
string promptName=“输入下一个矩形的名称:”;
string errorInvalid=“输入无效。在名称后面键入'rec',如果完成,则键入'stop'。”;
string errorUsed=“此名称已被使用!”;
名称中的字符串;
字符串名;
//展示欢迎横幅
欢迎();
/*提示用户输入第一个矩形或“停止”*/
bool read=read_rec(prompt1stName,errorInvalid,errorUsed,inName,list);
//而用户输入无效
while(read==false)
{
//显示“重试!”
这是错误的

/* Prompt user for first rectangle or 'stop' */
bool read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list);

// WHILE user input is invalid
while (read == false)
{

    // Display "Try again! "
    cout << "Try again! " << endl;
    bool read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list);
}
/*提示用户输入第一个矩形或“停止”*/
bool read=read_rec(prompt1stName,errorInvalid,errorUsed,inName,list);
//而用户输入无效
while(read==false)
{
//显示“重试!”

如果这是大量的代码,并且不符合本网站的问答风格,请尝试@mark:小心你的措辞-听起来像是在说“try Meta SO”()@阿拉赞:马克建议你标记你的帖子,并请版主将其移至代码审阅。哦……谢谢你纠正我!谢谢!修复了那个错误,你知道如何在输出错误无效时修复它吗?我相信它这样做是因为它不=rec,但用户没有输入我现在不知道。不确定,抱歉。我会使用调试器一步一步跟踪你的程序发生了什么。谢谢你的帮助!我现在就下载一个!
/* Prompt user for first rectangle or 'stop' */
bool read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list);

// WHILE user input is invalid
while (read == false)
{

    // Display "Try again! "
    cout << "Try again! " << endl;
    read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list);
}
for (;;)
{
    bool read = read_rec(prompt1stName, errorInvalid, errorUsed, inName, list);
    if (read)
        break;
    cout << "Try again! " << endl;
}