Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;主循环中的类循环_C++_Visual Studio_Function_Loops_Class - Fatal编程技术网

C++ C++;主循环中的类循环

C++ C++;主循环中的类循环,c++,visual-studio,function,loops,class,C++,Visual Studio,Function,Loops,Class,我需要帮助,以一种简单的方式解决我的代码中似乎无法正常工作的部分。我的“作业”的要点是,所有内容都必须在一个类中。现在我遇到的问题是,在我的代码的一部分,它假设“打印”n个产品,这意味着它显示了您在void get()部分中输入的内容,但我似乎无法解决的问题是,它只打印产品的姓氏、数量、重量,而不是所有文字 class Class { public: string name; int n, amount; float weight; void market(

我需要帮助,以一种简单的方式解决我的代码中似乎无法正常工作的部分。我的“作业”的要点是,所有内容都必须在一个类中。现在我遇到的问题是,在我的代码的一部分,它假设“打印”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;
        }
    };
};

必须使用
std::vector
(示例1)或数组在单个变量中保存多个数据(示例2)

例1 <>因为你已经用C++编程语言编写代码,所以强烈建议使用<代码> STD::向量< /代码>以获得最佳结果。 考虑课堂示例(也阅读注释):

例2 您只需稍加修改即可完成相同的操作,但拥有静态数字并不被视为动态的。如下所述,您可以为您的程序使用数组

而不是:

string name;
int n, amount;
float weight;
考虑
const int MAX=1024和使用(类变量必须仅在类内部可见,其他地方不可见):

编辑和工作示例类:

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

实际上,您正在覆盖
名称
金额
重量
。您必须使用类似于
std::vector

类{
公众:
std::矢量名称;
//其他人也一样
当您从
std::cin
获取它们时,必须将它们推入向量:

名称。向后推(名称);
金额。推回(金额);
重量。推回(重量);
打印时,在向量上循环:

for(int i=0;i小贴士:避免将类变量声明为公共变量,它们违反了OOP规则。但是您没有存储所有的
名称
权重
等。您应该使用
向量
来存储您需要的所有信息。此外,您的类不应该命名为
,这不是一个描述性名称。
市场如何因为这是C++,所以我强烈建议显示代码<>代码<矢量/>代码版本,而不是静态数组On.You.
可以分配/保留所需的大小,然后
get
中的代码将是相同的。@cigien添加了向量示例,请通知我是否有足够的信息。看起来不错。OP确实希望单独接受输入的数量,但我想答案中有足够的信息让他们知道。没有理由不这样做他希望数据是公开的,那么为什么不建议它是私有的呢?它也没有理由是私有的。我们不知道代码的最终目标是什么。我只是尊重他公开的决定。根据提供的代码,它没有理由是公开的。成员应该始终是私有的,除非有非常具体的原因不公开成为。
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
------------------------
string name;
int n, amount;
float weight;
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
------------------------