【问题标题】:C++ class loop in mainmain 中的 C++ 类循环
【发布时间】:2020-05-31 16:25:04
【问题描述】:

我需要帮助以一种简单的方式解决我似乎无法使其工作的部分代码。我的“任务”的重点是所有东西都必须尝试在一个类中。现在我遇到的问题在我的代码的一部分,它假设“打印”n 个产品,这意味着它显示您在 void get() 部分中输入的内容,但我似乎无法解决的问题是它只打印最后一个产品的名称、数量、重量,而不是所有文字。

   class Class
{
public:
    string name;
    int n, amount;
    float weight;
    void market()
    {
        cout << "Give the number of products you want to get at Market : " << endl;
        cin >> n;
    }
    void get()
    {
        for (int i = 0; i < n; i++)
        {
            cout << "Give product name,amount and weight : " << endl;
            cin >> name >> amount >> weight;
        }
    }
    void print()
    {
        cout << "\nProduct display:\n" << endl;
        for (int i = 0; i < n; i++)
        {
            cout << name << " - " << amount << " , " << weight << " kg" << endl;
            cout << "------------------------" << endl;
        }
    };
};

主要部分。

int main()
    {
            Class market;
            market.market();
            market.get();
            market.print();

    }

【问题讨论】:

  • 提示:避免将类变量声明为 public,它们违反了 OOP 规则。
  • 但是你还没有存储allnamesweights等。你应该使用vector来存储all您需要的信息。此外,您的班级不应命名为Class,这不是描述性名称。 Market 怎么样?

标签: c++ visual-studio function loops class


【解决方案1】:

您实际上是在覆盖nameamountweight。你必须使用类似于std::vector的东西:

class Class {
    public:
        std::vector<string> names;
        // Same for others

当您从std::cin 获取它们时,您必须将它们推入向量中:

names.push_back(name);
amounts.push_back(amount);
weights.push_back(weight);

打印时循环遍历向量:

for (int i = 0; i < n; i++) {
    cout << names[i] << " - " << amounts[i] << " , " << weights[i] << " kg" << endl;
    cout << "------------------------" << endl;
}

【讨论】:

  • 数据没有理由公开,为什么不建议将其设为私有呢?
  • 也没有理由将其设为私有。我们不知道代码的最终目标是什么。我只是尊重他公开的决定
  • 从提供的代码来看,它没有理由公开。成员应该始终是私有的,除非有一些非常具体的理由不这样做。
【解决方案2】:

您必须使用std::vector&lt;TYPE&gt;(示例 1),或在单个变量中保存多个数据的数组(示例 2)。

示例 1

由于您一直使用 C++ 编程语言进行编码,因此强烈建议使用 std::vector&lt;&gt; 以获得最佳效果。

考虑类示例(也可以阅读 cmets):

int count = 0; // PRIVATE SECTION
char ask;
std::vector<std::string> name;
std::vector<int> amount;
std::vector<float> weight;
std::string tName; // temp variables
int tAmount;
float tWeight;

public:

void market()
{
    cout << "WELCOME!" << endl; // nothing's required with vector
}
void get()
{
    do {
        cout << "Give product name, amount and weight : " << endl;
        cin >> tName >> tAmount >> tWeight; // getting temporary variables

        name.push_back(tName); // assigning
        amount.push_back(tAmount);
        weight.push_back(tWeight);

        count++;

        cout << "Add more? (Y/n): "; // add more? Go on if yes...
        cin >> ask;

    } while (ask == 'Y' || ask == 'y');
}
void print()
{
    cout << "\nProduct display:\n"
         << endl;
    for (int i = 0; i < count; i++)
    {
        cout << name[i] << " - " << amount[i] << " , " << weight[i] << " kg" << endl;
        cout << "------------------------" << endl;
    }
}

样本输出

WELCOME!
Give product name, amount and weight :
ABC 12 55.5
Add more? (Y/n): y
Give product name, amount and weight :
SSD 33 43.2
Add more? (Y/n): n

Product display:

ABC - 12 , 55.5 kg
------------------------
SSD - 33 , 43.2 kg
------------------------

示例 2

您只需进行少量修改即可完成相同的操作,但静态编号不被视为动态编号。您可以将数组用于您的程序,如下所述。

而不是:

string name;
int n, amount;
float weight;

考虑const int MAX = 1024; 并使用(类变量必须仅在类内部可见,其他任何地方都不可见):

private: // declare on top of the class ("private:" is by default and redundant)
    string name[MAX];
    int n, amount[MAX];
    float weight[MAX];

编辑和工作示例类:

void get()
{
    for (int i = 0; i < n; i++)
    {
        cout << "Give product name,amount and weight : " << endl;
        cin >> name[i] >> amount[i] >> weight[i];
    }
}
void print()
{
    cout << "\nProduct display:\n"
         << endl;
    for (int i = 0; i < n; i++)
    {
        cout << name[i] << " - " << amount[i] << " , " << weight[i] << " kg" << endl;
        cout << "------------------------" << endl;
    }
}

样本输出

Give the number of products you want to get at Market :
2
Give product name,amount and weight :
ABC 25 102
Give product name,amount and weight :
BDE 22 333

Product display:

ABC - 25 , 102 kg
------------------------
BDE - 22 , 333 kg
------------------------

【讨论】:

  • 因为这是 c++,我强烈建议显示代码的vector 版本,而不是静态数组。
  • 谢谢。此外,将其设为 first 示例,将数组版本设为第二个。许多人只会使用他们认为可行的第一个示例。
  • 好的,但不知道为什么会出现问题。此外,market 可以分配/保留所需的大小,然后get 中的代码将相同。
  • @cigien 添加了矢量示例,如果信息足够,请告诉我。
  • 看起来不错。 OP 确实希望单独接受输入的数量,但我认为答案中有足够的信息让他们弄清楚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-09
  • 2013-11-05
  • 2016-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多