如何将用户输入存储在与默认构造函数中的变量初始化不同的变量中?

时间:2021-03-03 15:45:02

Sorry for the lengthy title of this post. However, I believe it sums up the issue I am having. I have a default constructor that sets these defaults every time an object gets called:

对不起这篇文章的冗长标题。但是,我相信它总结了我遇到的问题。我有一个默认构造函数,每次调用一个对象时都会设置这些默认值:

Circles::Circles()
{
   radius = 1;
   center_x = 0;
   center_y = 0;
}

However, I want to give the user the option enter their own values. This would mean that the default values of radius, center_x and center_y must be ignored somehow. I set up the prompt like this:

但是,我想给用户提供输入自己值的选项。这意味着必须以某种方式忽略radius,center_x和center_y的默认值。我设置了这样的提示:

char enter;    // for user selection
    float rad = 1; // for user selection
    int x = 0, y = 0;     // for user selection

    cout << "Would you like to enter a radius for sphere2? (Y/N): ";
    cin.get(enter);

    if (toupper(enter) != 'N')
    {
        cout << "Please enter a radius: ";
        cin >> rad;
    }

    cout << endl;

    cout << "Would you like to enter a center for sphere2? (Y/N): ";
    cin.clear();
    cin.ignore();
    cin.get(enter);

    if (toupper(enter) != 'N')
    {
        cout << "Please enter x: ";
        cin >> x;
        cout << "Please enter y: ";
        cin >> y;
    }

    cout << endl << endl;

    if (toupper(enter) == 'Y')
        Circles sphere2(rad, x, y);
   Circles sphere2;

I want to pass rad, x, and y to this overloaded constructor:

我想将rad,x和y传递给这个重载的构造函数:

Circles::Circles(float r, int x, int y)
{
   radius = r;
   center_x = x;
   center_y = y;
}

This is how the output gets sent to the screen:

这是输出发送到屏幕的方式:

cout << "Sphere2:\n";
cout << "The radius of the circle is " << radius << endl;
cout << "The center of the circle is (" << center_x 
    << "," << center_y << ")" << endl;

At last, we arrive at the problem that the default values get printed:

最后,我们得出了打印默认值的问题:

The radius of the circle is 1 The center of the circle is (0,0)

圆的半径是1圆的中心是(0,0)

Why is this happening?

为什么会这样?

1 个解决方案

#1


if (toupper(enter) == 'Y')
        Circles sphere2(rad, x, y);
   Circles sphere2;

It creates local variable sphere2 at two different scopes (as if into two different functions). One at function scope, another at if-block scope. They are different. If-block variable will cease to exist (destruct) as soon as if-block is executed.

它在两个不同的范围创建局部变量sphere2(好像分成两个不同的函数)。一个在函数范围,另一个在if-block范围。它们是不同的。只要执行了if-block,if-block变量就会停止存在(destruct)。

Work with only one instance variable. You need to provide functions to Set the values. For example

仅使用一个实例变量。您需要提供设置值的功能。例如

Circles sphere;
sphere.SetX(x);
sphere.SetY(y);

The methods SetX and SetY will (should) set member-variable values of any already constructed instance.

SetX和SetY方法将(应该)设置任何已构造实例的成员变量值。

#1


if (toupper(enter) == 'Y')
        Circles sphere2(rad, x, y);
   Circles sphere2;

It creates local variable sphere2 at two different scopes (as if into two different functions). One at function scope, another at if-block scope. They are different. If-block variable will cease to exist (destruct) as soon as if-block is executed.

它在两个不同的范围创建局部变量sphere2(好像分成两个不同的函数)。一个在函数范围,另一个在if-block范围。它们是不同的。只要执行了if-block,if-block变量就会停止存在(destruct)。

Work with only one instance variable. You need to provide functions to Set the values. For example

仅使用一个实例变量。您需要提供设置值的功能。例如

Circles sphere;
sphere.SetX(x);
sphere.SetY(y);

The methods SetX and SetY will (should) set member-variable values of any already constructed instance.

SetX和SetY方法将(应该)设置任何已构造实例的成员变量值。